Week 4 Selection Structures Uni MAP Sem I1920

  • Slides: 32
Download presentation
Week 4 – Selection Structures Uni. MAP Sem I-19/20 DKT 121: Computer Programming 1

Week 4 – Selection Structures Uni. MAP Sem I-19/20 DKT 121: Computer Programming 1

Outline n n n n n Recall selection control structure Types of selection One-way

Outline n n n n n Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement Nested if Conditional operator Switch structure 2

Selection Statements n n n Used to control the flow of a program Also

Selection Statements n n n Used to control the flow of a program Also called as decision or branches Branches are conditions or choices used to enable selection of program flow 3

Recall. . Selection Structure n n Used to choose among alternative courses of action

Recall. . Selection Structure n n Used to choose among alternative courses of action C has three types: if, if. . else, and switch 4

Types of selection n n One-way selection = if Two-way selection = if. .

Types of selection n n One-way selection = if Two-way selection = if. . else Multi-selection Nested if {if dalam if} Switch structure = switch 5

1) The if selection structure n if structure is a single-entry/single-exit structure If student’s

1) The if selection structure n if structure is a single-entry/single-exit structure If student’s grade is greater than or equal to 60 Print “Pass” f. Grade >= 60 true print “Pass” false 6

One-way Selection = if n n n In C, a condition is represented by

One-way Selection = if n n n In C, a condition is represented by a logical (Boolean) expression true and false are logical (Boolean) values The syntax of one-way selection is: n n n if (expression) statement If the value of the expression is true, statement is executed; if false, statement is not executed and the computer goes on to the next statement in the program. 7

One-way Selection = if If student’s grade is greater than or equal to 60

One-way Selection = if If student’s grade is greater than or equal to 60 Print “Pass” f. Grade >= 60 true print “Pass” false 8

One-way Selection = if …. . if(f. Grade >= 60) printf(“Pass”); …. . 9

One-way Selection = if …. . if(f. Grade >= 60) printf(“Pass”); …. . 9

One-way Selection = if n Another example: char c. Grade; …… if(f. Markah>= 90)

One-way Selection = if n Another example: char c. Grade; …… if(f. Markah>= 90) c. Grade = 'A'; …… …. . . printf(“Grade is : %cn”, c. Grade); 10

One-way Selection = if n Another example: n if (temperature is greater than 70

One-way Selection = if n Another example: n if (temperature is greater than 70 degree and it is not raining) n recommended activity is golfing bool rain=false; … if((f. Temp > 70) && !(rain)) printf(“recommended activity is golfing”); 11

One-way Selection = if n Common Errors n if f. Score >= 90 //no

One-way Selection = if n Common Errors n if f. Score >= 90 //no parentheses c. Grade = 'A'; n if(f. Score >= 90); //; not here c. Grade = 'A'; 12

2) The if. . else selection structure n Specifies an action to be performed

2) The if. . else selection structure n Specifies an action to be performed both when the condition is true and when it is false If student’s grade is greater than or equal to 60 print “Pass” else print “Fail” Else part false print “Fail” f. Grade >= 60 true If part print “Pass” 13

Two-way Selection = if. . else n The syntax of two-way selection is: n

Two-way Selection = if. . else n The syntax of two-way selection is: n n n if (expression) statement 1; else statement 2; If the value of the expression is true, statement 1 is executed; if false, statement 2 is executed 14

Two-way Selection = if. . else If student’s grade is greater than or equal

Two-way Selection = if. . else If student’s grade is greater than or equal to 60 print “Pass” else print “Fail” false print “Fail” f. Grade >= 60 true print “Pass” 15

Two-way Selection = if. . else ……… if(f. Grade >=60) printf(“Pass”); else printf(“Fail”); ……

Two-way Selection = if. . else ……… if(f. Grade >=60) printf(“Pass”); else printf(“Fail”); …… 16

Two-way Selection = if. . else n Another example: if (f. Hour > 40.

Two-way Selection = if. . else n Another example: if (f. Hour > 40. 0) //Line 1 f. Wages = 40. 0 * f. Rate +1. 5 * f. Rate * (hour - 40. 0); //Line 2 else //Line 3 f. Wages = f. Hour * f. Rate; //Line 4 n n If f. Hour is 50, then the statement at Line 2 is executed If f. Hour is 30, then the statement at Line 4 is executed 17

3) Multi-selection = if-else if n The syntax is: if(expression 1 ) statement 1;

3) Multi-selection = if-else if n The syntax is: if(expression 1 ) statement 1; An “if…. else if” control structure shifts program control, step by step, through a series of statement blocks. else if(expression 2) statement 2; else if(expression 3) statement 3; … else statement n; 18

Multi-selection = if-else if n E. g. temp >30 0 c f. Temp >30

Multi-selection = if-else if n E. g. temp >30 0 c f. Temp >30 display hot false f. Temp > 20 20 -30 0 c mild false 10 -20 0 c cold f. Temp >10 <10 0 c very cold true Print “hot” Print “mild” Print “cold” false Print “very cold” 19

Multi-selection = if-else if if(f. Temp > 30) printf( “hotn”); else if(f. Temp >=20

Multi-selection = if-else if if(f. Temp > 30) printf( “hotn”); else if(f. Temp >=20 && f. Temp<=30) printf( “mildn”); else if(f. Temp >=10 && f. Temp < 20) printf(“coldn”); else printf( “very coldn”); 20

Compound (Block of) Statement n A compound statement (also called a block of statements)

Compound (Block of) Statement n A compound statement (also called a block of statements) takes the form of { n statement 1; statement 2; . . . statement n; } It is considered a single statement 21

Compound (Block of) Statement Example: if (i. Age > 18) { n printf("Eligible to

Compound (Block of) Statement Example: if (i. Age > 18) { n printf("Eligible to voten“); printf("No longer a minorn“); } else { } printf("Not eligible to voten“); printf(“Still a minorn”); 22

4) Nested if n n **lab Structure Task 3. 3 When one control statement

4) Nested if n n **lab Structure Task 3. 3 When one control statement is within another, it is said to be nested if(expression 1) if(expression 2) statement 1; OR n if(expression 1) { statement 1; if(expression 2) statement 2; } 23

Nested if n Example: if (f. Temperature >= 50) { if (f. Temperature >=

Nested if n Example: if (f. Temperature >= 50) { if (f. Temperature >= 80) printf( "Good day for swimming. n”); else printf( "Good day for golfing. n“); } else printf("Good day to play tennis. n“); 24

Nested if Another example #include <stdio. h> void main (void) { int i. Day;

Nested if Another example #include <stdio. h> void main (void) { int i. Day; float f. Time; n Output Type the day and time of interest Keyboard input 3 10. 00 Relax printf ("Type the day and time of interestnn"); scanf (" %d %f ", &i. Day, &f. Time); if (i. Day <= 5) { if (f. Time <= 9. 00) printf else printf } else { if (f. Time <= 8. 00) printf else printf } } (" Work nn"); (" Relax nn"); (" Sleep nn"); (" Have Fun nn"); 25

The Conditional Operator (? : ) n n n The syntax of using the

The Conditional Operator (? : ) n n n The syntax of using the conditional operator is: expression 1 ? expression 2 : expression 3; This is called a conditional expression. The statement: if (a >= b) max = a; else max = b; n Is equivalent to the statement: max = (a >= b) ? a : b; 26

5) switch Structures n n Similar to if-else if control structure The general form

5) switch Structures n n Similar to if-else if control structure The general form (syntax): switch (expression) { case value 1: statement 1; break; case value 2: statement 2; break; . . . case valuen: statementn; break; default: statements; } 27

switch Structures n n n The break statement has a special meaning and may

switch Structures n n n The break statement has a special meaning and may or may not appear after each statement. In C, switch, case, break, and default are reserved words. In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the corresponding action. 28

switch Structures n n n The expression is usually an identifier. The value of

switch Structures n n n The expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector. Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement. 29

switch Structures n **lab Structure Task 3. 4 Example: switch (c. Grade) { }

switch Structures n **lab Structure Task 3. 4 Example: switch (c. Grade) { } n case 'A': printf("The grade is A. “); break; case 'B': printf("The grade is B. “); break; case 'C': printf("The grade is C. “); break; case 'D': printf("The grade is D. “); break; case 'F': printf("The grade is F. “); break; default: printf("The grade is invalid. “); where, c. Grade is a variable of the type char. If the value of c. Grade is, say 'A', the output is The grade is A. 30

switch Structures n The switch statement executes according to the following rules: n n

switch Structures n The switch statement executes according to the following rules: n n n When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. A break statement causes an immediate exit from the switch structure 31

What’s wrong? ? 32

What’s wrong? ? 32