Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection in C.

Similar presentations


Presentation on theme: "Selection in C."— Presentation transcript:

1 Selection in C

2 If Statement: if ( condition) statement(s); One way decision:
Referred to as “Open Branch” in the book. Syntax: Flowchart: if ( condition) statement(s); Condition? true Statement(s) false Rest of the program

3 If / else statement: 2-way decision: Syntax: Flowchart: if (condition)
statement(s); else Condition? false true Statement(s) Statement(s) Rest of the program

4 Exercise: Write a C program to..
Ask the user to enter his/her age. Check the age, if it is > 21, display “ You may drink.”, otherwise, display “Do not drink” regardless of the age, Display “Do not Drink and Drive”.

5 Explain the solution, using flowchart:
Start Declare variable: age Get user’s age Age > 21? false true May not drink You may drink Don’t drink & drive end

6 Explain the solution using pseudo code:
Declare variable to store age. Prompt the user to enter age, Read the entered value into variable If age is > 21 then Display you may drink Otherwise Display you may not drink 5. Display: Do not drink and drive

7 Translate the logic into C code:

8 Nested If Statement: An if statement can be nested in the if part or the else part of the if statement. if (condition) …. else How can you tell where an if statement begins or ends?

9 Ex1: Assume that: int a = 5, b = 7, c = 10;
Rule: else binds with the closest if Ex1: Assume that: int a = 5, b = 7, c = 10; if (a < b) if (c <= a + b) printf( " First place \n“); else printf( "2nd place \n“); printf (“3rd place \n”); Output:

10 Dangling else: Determine which if the else binds with?
Ex2: Assume: a = 12, b = 2, c = 10 if (a < b) if (c <= a + b) printf( " First place \n“); else printf( "2nd place \n“); Output:

11 Ex: What is the outcome? float score = 75; float gpa = 2.5;
if (score > 80) if (gpa > 2.0) printf (“xxx”); else printf(“ ###”);

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

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

14 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)

15 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);

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

17 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

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

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

20 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 < 20000) || (age > 60) ) printf (“ Give promotion..”); else printf (“ Does not qualify!”);

21 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) is the same as (a >= b) ! (a == b) is the same as (a != b)

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

23 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 "Selection in C."

Similar presentations


Ads by Google