140 likes | 296 Views
EECE 1207 Fall 2005. Lecture 4 More conditionals. Topics. Compound if statements Nested if statements If – else chains Switch statements DeMorgan’s law. Multiple actions. What if there’s more than one conditional action? Ex:
E N D
EECE 1207 Fall 2005 Lecture 4 More conditionals Salem EECE1207
Topics • Compound if statements • Nested if statements • If – else chains • Switch statements • DeMorgan’s law Salem EECE1207
Multiple actions • What if there’s more than one conditional action? • Ex: “If your temperature is high, then you have a fever and should take 2 aspirins, then go to bed, and call in sick tomorrow” Salem EECE1207
Compound statements • Group together statements so that they are treated as a single statement: if (condition) { statement1; statement2; … } • Also called a “block” • Useful not just in conditionals, but also in other structures in C Salem EECE1207
Compound statements if (temperature > 98.5) { printf (“you have fever. \n”); aspirin = aspirin –2; printf (“go to bed. \n”); printf (“call in sick tomorrow. \n”); } Salem EECE1207
if (DangerLevel > 5){ openhatch(); ejectseat(); } if (DangerLevel > 5) openhatch(); ejectseat(); What if { } are omittedAnother dramatic example Salem EECE1207
Compound if-else if (Nhours<=40) { salary = Nhours*rate; printf (“no overtime. salary = %f\n”, salary ); } else { salary = 40*rate + overtime(); printf (“overtime. salary = %f\n”, salary ); } Salem EECE1207
Flow chart for compound if-else NHours<=40 Yes No • Statement • Statement • Statement • Statement • Statement • Statement Next statements Salem EECE1207
if (gpa >= 3.8) printf (“Magna Cum Laude”); else if (gpa >= 3.0) printf (“Summa Cum Laude”); else if (gpa >= 2.0) printf (“Good standing”); else printf (“I ETA PI”); printf (“end of transcript”); Compound if-else statements Yes Output “Magna Cum Laude” gpa >= 3.8 No Yes Gpa >=3.0 Output “Summa Cum Laude” No Yes Output “I ETA PI” Gpa >=2.0 No Output “on probation” Output “end of transcript” Salem EECE1207
Switch statements Yes switch (m_status){ case ‘s’ : printf (“single”); break; //optional but recommended case ‘m’: printf (“married”); break; //optional but recommended case ‘w’ : printf (“widowed”); break; //optional but recommended Default: //optional but recommended printf (“status unknown”); } printf (“end of decoding marital status”); m_status =‘s’ Output “single” No Yes m_status =‘m’ Output “married” No Yes m_status =‘w’ Output “widowed” No Output “status unknown” Output “end of decoding marital status” Salem EECE1207
Switch statementsmultiple decisions Switch (m_status){ case ‘s’ : case ‘S’ : printf (“single”); break; //optional but recommended case ‘m’: case ‘M’ : printf (“married”); break; //optional but recommended case ‘w’ : case ‘W’ : printf (“widowed”); break; //optional but recommended Default: //optional but recommended printf (“status unknown”); } printf (“end of decoding marital status”); Yes ‘s’ or ‘S’ Output “single” No Yes ‘m’ or ‘M’ Output “married” No Yes ‘w’ or ‘W’ Output “widowed” No Output “status unknown” Output “end of decoding marital status” Salem EECE1207
Switch with no breaks? Switch (m_status){ case ‘s’ : case ‘S’ : printf (“single”); case ‘m’: case ‘M’ : printf (“married”); case ‘w’ : case ‘W’ : printf (“widowed”); Default: //optional but recommended printf (“status unknown”); } printf (“end of decoding marital status”); Yes ‘s’ or ‘S’ Output “single” No Yes ‘m’ or ‘M’ Output “married” No Yes ‘w’ or ‘W’ Output “widowed” No Output “status unknown” Output “end of decoding marital status” Salem EECE1207
Switch • Switch is used ONLY with integers or characters • IS not used with floats/doubles Salem EECE1207
Continued on blackboard Salem EECE1207