Lec 5.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Introduction to Computers and Programming Class 15 Introduction to C Professor Avi Rosenfeld.
CS 201 Selection Structures (2) and Repetition
To remind us We finished the last day by introducing If statements Their structure was:::::::::
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
A. Abhari CPS1251 Topic 4: Control structures Control Structures Overview Conditions if Statement Nested if Statements switch Statement Common Programming.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Tutorial ICS431 – Introduction to C.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Chapter 5: Structured Programming
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Chapter 5: Structured Programming
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Repetition statements
CSE1301 Sem Selection Lecture 25 Lecture 9: Selection.
Chapter 5: Structured Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part I
Chapter 4: Control Structures I
Decisions Chapter 4.
Lecture 7: Repeating a Known Number of Times
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Logical Statements and Selection Structures
Control Structures Lecture 7.
Looping.
Chapter 4: Control Structures I
Decision Making.
Compound Assignment Operators in C++
Lec 7.
SELECTION STATEMENTS (2)
Chapter 5 Decision Making and Branching
Lec 6.
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Lecture4.
Chapter#3 Structured Program Development in C++
Computer Programming Techniques Semester 1, 1998
SELECTIONS STATEMENTS
Week 3 – Program Control Structure
Perfect squares class identifySquareButLessClever {
Structured Program Development in C++
DATA TYPES There are four basic data types associated with variables:
Lecture 4: Selection Control Structure
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
FUNCTION ||.
ICS103: Programming in C 4: Selection Structures
ICS103: Programming in C 5: Repetition and Loop Statements
Switch Case Structures
Chapter 13 Control Structures
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

Lec 5

Lecture Outline Multiple Alternative if Counters Nested If statement Switch Statement

Review of Syntax Simple if Statement if (condition) statement; if (condition) statement; else statement; Compound if Statement if (condition) {statements} if (condition) {statements} else {statements}

Exercise Write an interactive program that contains an if statement that may be used to compute the area of a square (area = side²) or a triangle (area = 1/2 * base * height) after prompting the user to type the first character of the figure name (S or T).

Multiple Alternative Decision Syntax if (condition) statement; else if (condition) statement; : else statement;

alternative if statement Comparing if Statements if (score>=90) printf("A"); if (score>=80 && score<90) printf("B"); if (score>=70 && score<80) printf("C"); if (score>=60 && score<70) printf("D"); if (score<60) printf("F"); 5 simple if statements if (score>=90) printf("A"); else if (score>=80) printf("B"); else if (score>=70) printf("C"); else if (score>=60) printf("D"); else printf("F"); 1 multiple alternative if statement

Exercise 1 Write a program that accepts a score from the user and displays the grade for that score. Example: if the user enters a 75, the program will display "C".

Exercise 1 #include<stdio.h> int main(void) {float score; printf("Please enter a score: "); scanf("%f", &score); if (score>=90) printf("A\n"); else if (score>=80) printf("B\n"); else if (score>=70) printf("C\n"); else if (score>=60) printf ("D\n"); else printf("F\n"); return(0);}

Counters A counter is a variable whose value is either added to or subtracted from to achieve a result. An increment counter is a variable whose value is added to. A decrement counter is a variable whose value is subtracted from. All counters MUST be initialized before using. All counters MUST be of type int. Examples: x=x+1; stock=stock-1; hot=hot+1; x=x-1; stock=stock-100; cold=cold+1;

Nested if statement A nested if is an if statement with another if statement as its true task. A real nested if is found in the true task of an if statement and not the false task. If an if statement is found in the false task then it’s a multiple alternative if. if (condition) if (condition) statement; else statement; else statement; Syntax

Example true true Wet roads Message Status='S' temp>0 false false Icy roads Message Drive carefully Message

Example #include<stdio.h> /* a program to determine road conditions */ int main(void) {char status; float temp; printf("Please enter the road status: "); scanf("%c",&status); if (status=='S' || status=='s') {printf("Please enter the temperature: "); scanf("%f", &temp); if (temp>0) printf("Wet Roads!\n"); else printf ("Icy Roads!\n");} else printf ("Drive carefully\n"); return (0);}

Example 2 true true Wet roads Message Status='S' temp>0 false false Drive carefully Message

Example 2 if (status=='S' || status=='s') if (temp>0) printf("Wet Roads!\n"); else printf ("Drive carefully\n"); if (status=='S' || status=='s') {if (temp>0) printf("Wet Roads!\n");} else printf ("Drive carefully\n");

Switch Statement switch (controlling expression) { label_set_1: Syntax switch (controlling expression) { label_set_1: statements_1 break; label_set_2: statements_2 . Label_set_n: statements_n default: statements_d } Selection Structures 2

Example 1 printf("Please enter a color code (R, B, or Y): "); scanf("%c",&color); switch (color) { case 'R': printf("Red\n"); break; case 'B': printf("Blue\n"); case 'Y': printf("Yellow\n"); } Selection Structures 3

Example 2 printf("Enter watts (25, 40, 60, 75, or 100): "); scanf("%i",&watts); switch (watts) { case 25: life=2500; break; case 40: case 60: life=1000; case 75: case 100: life=750; default: life=0; } printf("The life of your watts is %i\n",life); Selection Structures 4

Example 3 printf("Please enter your grade: "); scanf("%c",&grade); switch (grade) { case 'A': case 'a': printf("Excellent!\n"); break; case 'B': case 'b': printf("Very Good!\n"); case 'C': case 'c': printf("Fair.\n"); default: printf("Poor\n");} Selection Structures 5

Example 4 Write a program that reads a number from the user and determines if it's a positive or negative number. Can this problem be implemented using a switch statement? #include<stdio.h> int main(void) {int number; printf("Please enter a number: "); scanf("%i", &number); if (number>=0) printf("Positive\n"); else printf("Negative\n"); return(0);} Selection Structures 11

Comparison of if Statements and The Switch Statement if statements are more general than the switch statement. Case labels that contain type float values or strings are not permeated in switch statements. The switch statement is more readable in many contexts. Thus, it should be used whenever practical. Selection Structures 12

Switching the values of two variables Exercise 1 Write a program that switches the values of two variables. printf ("The value of x is %i", y); printf ("The value of y is %i", x); temp = x; x = y; y = temp;

Solution #include<stdio.h> int main(void) {int first, second, temp; printf("Please enter two numbers: "); scanf("%i%i", &first, &second); printf("The value of first number is %i\n", first); printf("The value of second number is %i\n", second); temp=first; /* Switch them */ first=second; second=temp; printf("The value of first number after switching is %i\n", first); printf("The value of second number after switching is %i\n", second); return(0);}

Exercise 2 Write a program that reads an integers from the user, then finds & prints: The value of the (units) digit. The value of the (tens) digit. The value of the (hundreds) digit.

Solution #include <stdio.h> int main (void) {int number, units, tens, hundreds; printf("Please enter a number: "); scanf("%i", &number); units=number%10; tens=(number%100)/10; hundreds=(number%1000)/100; printf("Units = %i\n", units); printf("Tens = %i\n", tens); printf("Hundreds = %i\n", hundreds); return(0);}