html5-img
1 / 14

EECE 1207 Fall 2005

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:

Download Presentation

EECE 1207 Fall 2005

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EECE 1207 Fall 2005 Lecture 4 More conditionals Salem EECE1207

  2. Topics • Compound if statements • Nested if statements • If – else chains • Switch statements • DeMorgan’s law Salem EECE1207

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

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

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

  6. if (DangerLevel > 5){ openhatch(); ejectseat(); } if (DangerLevel > 5) openhatch(); ejectseat(); What if { } are omittedAnother dramatic example Salem EECE1207

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

  8. Flow chart for compound if-else NHours<=40 Yes No • Statement • Statement • Statement • Statement • Statement • Statement Next statements Salem EECE1207

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

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

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

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

  13. Switch • Switch is used ONLY with integers or characters • IS not used with floats/doubles Salem EECE1207

  14. Continued on blackboard Salem EECE1207

More Related