Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege.

Similar presentations


Presentation on theme: "Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege."— Presentation transcript:

1 Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr

2 Topics Type int as Boolean Type int as Boolean Nested if statements Nested if statements switch statement switch statement

3 Recall : Syntax for if statement if (condition) statement;OR { statement 1 ; … statement n ; }

4 Recall : Syntax for if & else statement if (condition) statement T ; else statement F ; OR if (condition) { statement T1 ; … statement Tn ; }else{ statement F1 ; … statement Fn ; }

5 Recall : Syntax for cascaded if statements if (condition 1 ) statement 1 ; else if (condition 2 ) statement 2 ;... else if (condition n ) statement n ; else statement e ;

6 Type int as Boolean In C, integers can be used as Booleans In C, integers can be used as Booleans Integer value 0 is false Integer value 0 is false Any non-zero integer value is true Any non-zero integer value is true

7 #include #include /* Test some Booleans. */ int main() { int whatever = 0; int whatever = 0; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

8 #include #include /* Test some Booleans. */ int main() { int whatever = 1; int whatever = 1; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

9 #include #include /* Test some Booleans. */ int main() { int whatever = -100; int whatever = -100; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

10 #include #include /* Test some Booleans. */ int main() { int whatever = 0.003; int whatever = 0.003; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Example: What is the output?

11 #include #include /* Test some Booleans. */ int main() { float whatever = 0.003; float whatever = 0.003; if (whatever) if (whatever) { printf(“Is that true, Homer?\n”); printf(“Is that true, Homer?\n”); } else else { printf(“No, Marge.\n”); printf(“No, Marge.\n”); } return 0; return 0;} Behavior is “undefined”

12 Nested if statements We can nest if statements (one if statement inside another) to code decisions with multiple alternatives We can nest if statements (one if statement inside another) to code decisions with multiple alternatives

13 Example: Nested if if (num > 0) printf(“positive”);else if (num < 0) printf(“negative”); else /* num is zero */ printf(“zero”);

14 Example: Nested if if(age<2) printf(“Infant”); elseif(age<18) printf(“Child”); else /* age >= 18 */ printf(“Adult”);

15 /* Print a message if all criteria are met */ if (marital_status == ‘S’) if (gender == ‘M’) if (age >= 18 && age = 18 && age <= 26) printf(“All criteria are met.”); IS EQUIVALENT TO if (marital_status == ‘S’ && gender == ‘M’&& age >= 18 && age = 18 && age <= 26) printf(“All criteria are met.”); Example: Nested if

16 Notes on Cascaded if if (number >= 5) { if(number >= 10) if(number >= 10){ if (number < 20) printf(“S1”);elseprintf(“S2”);}elseprintf(“S3);printf(“S4”);}elseprintf(“S5”); What is the output if: number is equal to 3 number is equal to 5 number is equal to 10 number is equal to 35 Q:

17 Common Mistake if(age>2) /* if greater than 2 */ if(age<18) /* and less than 18 */ printf (“Child”); /* it's a child */ else printf( “Infant”); /* ERROR: inappropriate response */ if(age>2) /* if greater than 2 */ { if(age<18) /* and less than 18 */ printf (“Child”); /* it's a child */ } else printf( “Infant”);

18 CONDITIONAL OPERATOR Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if- else condition phrases. Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if- else condition phrases. Conditional operator’s shape is: Conditional operator’s shape is: –Condition_phrase ? phrase1 : phrase2; If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value. If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value.

19 Example - CONDITIONAL OPERATOR Example - CONDITIONAL OPERATOR int a, b, c;... c = a > b ? a : b; IS THE SAME AS if(a > b) c = a; else c = b;

20 Multiple Choice: switch and break If choosing more than 2 alternatives you have use if…else if…else If choosing more than 2 alternatives you have use if…else if…else Another alternative is the switch statement Another alternative is the switch statement

21 switch example switch(day){ case 1: printf(“Monday”); break; case 2: printf(“Tuesday”); break; case 3: printf(“Wednesday”); break; case 4: printf(“Thursday”); break; case 5: printf(“Friday”); break; case 6: printf(“Saturday”); break; case 7: printf(“Sunday”); break; case default: printf(“Day number should be between 1 and 7”); }

22 Syntax for switch statement switch(controlling expression) { label set 1 : statements 1 ; break; label set 2 : statements 2 ; break;... label set n : statements n ; break;default: statements d ; }

23 Program flow in switch statements

24 switch example switch(class){ case ‘B’: case ‘b’: printf(“Battleship”); break; case ‘C’: case ‘c’: printf(“Cruiser”); break; case ‘D’: case ‘d’: printf(“Destroyer”); break; case ‘F’: case ‘f’: printf(“Frigate”); break; default: printf(“unknown ship class %c”,class); }

25 Notes on switch statement Controlling expression may be of type int or char, but not float or double Controlling expression may be of type int or char, but not float or double An equivalent statement using if…else if statements can be written for each switch statement, but not vice versa An equivalent statement using if…else if statements can be written for each switch statement, but not vice versa

26 switch example switch(month){ case 3: case 4: case 5: printf(“Spring”); break; case 6: case 7: case 8: printf(“Summer”); break; case 9: case 10: case 11: printf(“Autumn”); break; case 12: case 1: case 2: printf(“Winter”); break; default: printf(“month must be between 1 and 12”); }

27 Same example using if…else if statements if (month==3 || month==4 || month==5) printf(“Spring”); else if (month==6 || month==7 || month==8) printf(“Summer”); else if (month==9 || month==10 || month==11) printf(“Autumn”); else if (month==12 || month==1 || month==2) printf(“Winter”);else printf(“month must be between 1 and 12”); }

28 switch and if else When should you use a switch and when should you use the if else construction? When should you use a switch and when should you use the if else construction? –Often you don't have a choice. You can't use a switch if your choice is based on evaluating a float or a double variable or expression You can't use a switch if your choice is based on evaluating a float or a double variable or expression Nor can you conveniently use a switch if a variable must fall into a certain range. It is simple to write the following: Nor can you conveniently use a switch if a variable must fall into a certain range. It is simple to write the following: –if (integer 2)

29 Summary Type int as Boolean Type int as Boolean Nested if statements Nested if statements switch statement switch statement


Download ppt "Introduction to Computing Lecture 05: Selection (continued) Introduction to Computing Lecture 05: Selection (continued) Assist.Prof.Dr. Nükhet ÖZBEK Ege."

Similar presentations


Ads by Google