23
1 Selection in C

1 Selection in C. 2 If Statement: One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

1

Selection in C

Page 2: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

2

If Statement:

One way decision: Referred to as “Open Branch” in the book. Syntax: Flowchart:

if ( condition) statement(s);

Condition? true

Statement(s)

Rest of the program

false

Page 3: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

3

If / else statement:

2-way decision: Syntax: Flowchart: if (condition) statement(s); else

statement(s);Condition?

Statement(s)Statement(s)

Rest of the program

false true

Page 4: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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”.

Page 5: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

5

Explain the solution, using flowchart:Flowchart: Start

Declare variable: age

Get user’s age

Age > 21?

You may drinkMay not drink

Don’t drink & drive

end

truefalse

Page 6: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

6

Explain the solution using pseudo code:Pseudo code:

1. Declare variable to store age.

2. Prompt the user to enter age,

3. Read the entered value into variable

4. If age is > 21 then1. Display you may drink

Otherwise Display you may not drink

5. Display: Do not drink and drive

Page 7: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

7

Translate the logic into C code:

Page 8: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

8

Nested If Statement: An if statement can be nested in the if part or the else part of

the if statement. if (condition) if (condition) …. else … else if (condition) … How can you tell where an if statement begins or

ends?

Page 9: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

9

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

else

printf (“3rd place \n”);

Output:

Page 10: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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:

Page 11: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

11

Ex: What is the outcome? float score = 75;

float gpa = 2.5;

if (score > 80)

if (gpa > 2.0) printf (“xxx”); else printf(“ ###”);

Page 12: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

12

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 …

Page 13: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

13

Improved style more clarity Such nested if / else can be written as:

if (condition)

else if (condition)

else if (condition)

….

else

Page 14: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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

Page 15: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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

Page 16: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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 True True

True False False

False True False

False False False

Page 17: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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

Page 18: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

18

Logical or: ||C1 (condition1) C2 (condition2) C1 || C2

True True True

True False True

False True True

False False False

Page 19: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

19

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 < 0) || (test > 100) ){ printf (“ Invalid test score..”); printf (“ Program will end now.”); return 1; }

Page 20: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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!”);

Page 21: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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)

Page 22: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

22

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

Page 23: 1 Selection in C. 2 If Statement:  One way decision: Referred to as “Open Branch” in the book. Syntax:Flowchart: if ( condition ) statement(s); Condition?

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