Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.

Similar presentations


Presentation on theme: "1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else."— Presentation transcript:

1 1 Selection in C

2 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else if (condition) …. else …

3 3 Improved style  more clarity  Such nested if / else can be written as: if (condition) … else if (condition) … …. else …

4 4 Example: Examine the student’s test score and assign a letter grade: int score = … char letter; if (score >= 90) { letter = ‘A’; printf(“ Great job! \n”); } else if (score >= 80) letter = ‘B’; else if (score >= 70) …

5 5 Rewrite the code with improved style: if (score >= 90) { letter = ‘A’; printf(“ Great job! \n”); } else if (score >= 80) letter = ‘B’; else if (score >= 70) letter = ‘C’; else if (score >= 60) letter = ‘D’; else letter = ‘F’; Printf(“ Your letter grade is: %c”, letter); …

6 6 True / false in C:  How does the computer store true or false internally? True is stored as 1 False is stored as 0 Ex: what is the outcome? printf (“%d”, 5 < 100);

7 7 More complex conditions: Stopped  You may combine 2 or more conditions using logical operators:  Logical Operators:  Logical and: && C1 (condition1)C2 (condition2)C1 && C2 True False TrueFalse

8 8 Application:  Check the range of the data: Ex: A healthy heart rate is between 60 and 85. Check the user’s heart rate, and inform the user whether s/he is healthy or not int rate; printf (“ Enter your heart rate: “); Scanf(“%d”, &rate); if ((rate >= 60) && (rate <= 85 ) ) … else

9 9 Logical or: || C1 (condition1)C2 (condition2)C1 || C2 True FalseTrue FalseTrue False

10 10 Applications:  1) Checking for invalid rage: Ex1: Check the range of the test score. If it is outside the range of 0.. 100 end the program: int test; test = 77; if ( (test 100) ) { printf (“ Invalid test score..”); printf (“ Program will end now.”); return 1; }

11 11 Cont… Ex2: Assume that a company is considering a promotion for employees older than 60, or those who are paid under $20,000. Assume variables: salary and age are declared with some values stored in them. If ( (salary 60) ) printf (“ Give promotion..”); else printf (“ Does not qualify!”);

12 12 Cont…  Logical not ! Operates on one condition. It negates the value of the condition.  Ex1: ! (120 < 100)   Ex2: int veteran = 1; !veteran   Notice: ! (a = b) ! (a == b) is the same as (a != b)

13 13 Precedence Rule:  (..)  Unary minus, !  * / %  + - , >=  ==, !=  &&  ||  = assignment operator

14 14 Evaluate the following expressions: Assume int a = 5, b = 10, c = 15, flag = 1;  a < b && b < c  flag && b > 10  !(b <= 12) && (a %2 == 0)  !flag || c > a  int answer; answer = (a > b) || (c > b); Printf (“%d”,answer);  C >= a + b


Download ppt "1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else."

Similar presentations


Ads by Google