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
Topics Type int as Boolean Nested if statements switch statement
Recall : Syntax for if statement if (condition) statement; OR if (condition) statement 1 ; statement n ;
Recall : Syntax for if & statement if (condition) statement T ; statement F ; OR if (condition) statement T1 ; statement Tn ; statement F1 ; statement Fn ;
Recall : Syntax for cascaded if statements if (condition 1 ) statement 1 ; if (condition 2 ) statement 2 ;... if (condition n ) statement n ; statement e ;
Type int as Boolean In C, integers can be used as Booleans Integer value 0 is false Any non-zero integer value is true
Example: What is the output? #include <stdio.h> /* Test some Booleans. */ int main() int whatever = 0; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;
Example: What is the output? #include <stdio.h> /* Test some Booleans. */ int main() int whatever = 1; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;
Example: What is the output? #include <stdio.h> /* Test some Booleans. */ int main() int whatever = -100; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;
Example: What is the output? #include <stdio.h> /* Test some Booleans. */ int main() int whatever = 0.003; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;
Example: What is the output? #include <stdio.h> /* Test some Booleans. */ Behavior is undefined int main() float whatever = 0.003; if (whatever) printf( Is that true, Homer?\n ); printf( No, Marge.\n ); return 0;
Nested if statements We can nest if statements (one if statement inside another) to code decisions with multiple alternatives
Example: Nested if if (num > 0) printf( positive ); if (num < 0) printf( negative ); /* num is zero */ printf( zero );
Example: Nested if if(age<2) printf( Infant ); if(age<18) printf( Child ); /* age >= 18 */ printf( Adult );
Example: Nested if /* Print a message if all criteria are met */ if (marital_status == S ) if (gender == M ) if (age >= 18 && age <= 26) printf( All criteria are met. ); IS EQUIVALENT TO if (marital_status == S && gender == M && age >= 18 && age <= 26) printf( All criteria are met. );
Notes on Cascaded if Q: 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 if (number >= 5) if(number >= 10) if (number < 20) printf( S1 ); printf( S2 ); printf( S3); printf( S4 ); printf( S5 );
Common Mistake if(age>2) /* if greater than 2 */ if(age<18) /* and less than 18 */ printf ( Child ); /* it's a child */ 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 */ printf( Infant );
CONDITIONAL OPERATOR Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if condition phrases. 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.
Example - CONDITIONAL OPERATOR int a, b, c;... c = a > b? a : b; IS THE SAME AS if(a > b) c = a; c = b;
Multiple Choice: switch and break If choosing more than 2 alternatives you have use if if Another alternative is the switch statement
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 );
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 ;
Program flow in switch statements
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);
Notes on switch statement Controlling expression may be of type int or char, but not float or double An equivalent statement using if if statements can be written for each switch statement, but not vice versa
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 );
Same example using if if statements if (month==3 month==4 month==5) printf( Spring ); if (month==6 month==7 month==8) printf( Summer ); if (month==9 month==10 month==11) printf( Autumn ); if (month==12 month==1 month==2) printf( Winter ); printf( month must be between 1 and 12 );
switch and if When should you use a switch and when should you use the if 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 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 < 1000 && integer > 2)
Summary Type int as Boolean Nested if statements switch statement