1 / 25

Lecture 7: if-else if-else Statements

CSC 107 – Programming For Science. Lecture 7: if-else if-else Statements. if (…) statement. First evaluates expression in parenthesis If expression is true , executes next statement Skips over the statement , when expression is false

fawn
Download Presentation

Lecture 7: if-else if-else Statements

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. CSC 107 – Programming For Science Lecture 7:if-else if-else Statements

  2. if (…) statement • First evaluates expression in parenthesis • If expression is true, executes next statement • Skips over the statement, when expression is false inthst = 30, dc = 27;bool gang = (hst > 12) && (dc == 27); if (nyc == dc)cout << “You lie" << endl;if (!gang)cout << "That ain't right." << endl;if (dc > 20);cout << "Huh?"

  3. if (…) statement • First evaluates expression in parenthesis • If expression is true, executes next statement • Skips over the statement, when expression is false • This leads to one very common bug • ;is statement (a boring statement, but…) if (dc > 20);cout << "Huh?"

  4. I Want More! • Add opening brace ({) after closing parenthesis • Can now write all statements to execute • Add closing brace (}) to show where ifends • If expression false, execution restarts at that point if (sqrt(x) == 3.0) {cout << "root of x = 3" << endl; cout << "So, x = 9" << endl;}

  5. true false else statement • Often want to perform one of two actions • Execute some statements when condition is true • But when it is false, do some other work • Continue as normal afterward,no matter the path • In these cases, can use if-else statement • elsemust come immediately after if's ; or } • Like if, elseshould always have {} • Cannot have elsewithout ifto start process • In C++, there is no problem with if & no else

  6. if - else Examples if (value > 0) {cout << "Its positive" << endl;} else { value = abs(value);}if ((year % 4 == 0) && ((year % 100) != 0)) {daysInYear = 366;cout << "Not quite right, but close enough";cout << endl;leapYear = true;} else {leapYear = false;daysInYear = 365; }

  7. Many Choices • May want to choose from many possibilities

  8. Many Choices • May want to choose from many possibilities • Could nest if – elsestatements inside one another if (a > 3) {cout << "a greater than 3" << endl;} else { if (b > 10) {cout << "b greater than 10" << endl; } else { if (c == -23) {cout << "c is equal to -23" << endl; } else {cout << "This really sucks" << endl;cout << "Sorry its stupid" << endl; } }}

  9. Many Choices • May want to choose from many possibilities • Could nest if – elsestatements inside one another • Or easier & nicer if – else if – else statement if (a > 3) {cout << "a greater than 3" << endl;} else if (b > 10) {cout << "b greater than 10" << endl;} else if (c == -23) {cout << "c is equal to -23" << endl;} else {cout << "This really sucks" << endl;cout << "Sorry its stupid" << endl;}

  10. if – else if – else Usage • Must begin with ifstatement at the start • This is required; what would we be saying elseto? • Onlyrequired part of this entire process • Can then have zero or more else ifs • Tests can be anything; do not have to be related • Until one is true, will be examined one-by-one • Execute 1st clause where true expression is found • Only at the very end can have else clause • If nothing else matches then elseis executed

  11. if – else if – else Example if (wealth > 1000000) {cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) {cout << "Wow, you're comfortable" << endl;} else {cout << "Government bailout!" << endl;} else if (wealth > -100000) {cout << "File for bankruptcy" << endl;} else if (wealth > 10000) {cout << "Could be worse" << endl;} else if (wealth > 0) {cout << "Good luck" << endl;}

  12. if – else if – else Example if (wealth > 1000000) {cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) {cout << "Wow, you're comfortable" << endl;} else {cout << "Government bailout!" << endl;} else if (wealth > -100000) {cout << "File for bankruptcy" << endl;} else if (wealth > 10000) {cout << "Could be worse" << endl;} else if (wealth > 0) {cout << "Good luck" << endl;}

  13. if – else if – else Example if (wealth > 1000000) {cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) {cout << "Wow, you're comfortable" << endl;} else if (wealth > -100000) {cout << "File for bankruptcy" << endl;} else if (wealth > 10000) {cout << "Could be worse" << endl;} else if (wealth > 0) {cout << "Good luck" << endl;} else {cout << "Government bailout!" << endl;}

  14. if – else if – else Example if (wealth > 1000000) {cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) {cout << "Wow, you're comfortable" << endl;} else if (wealth > -100000) {cout << "File for bankruptcy" << endl;} else if (wealth > 10000) {cout << "Could be worse" << endl;} else if (wealth > 0) {cout << "Good luck" << endl;} else {cout << "Government bailout!" << endl;}

  15. if – else if – else Example if (wealth > 1000000) {cout << "Can I borrow a buck?" << endl;} else if (wealth > 100000) {cout << "Wow, you're comfortable" << endl;} else if (wealth > 10000) {cout << "Could be worse" << endl;} else if (wealth > 0) {cout << "Good luck" << endl;} else if (wealth > -100000) {cout << "File for bankruptcy" << endl;} else {cout << "Government bailout!" << endl;}

  16. Spacing in a Program • C++ ignores spaces in a program • This also means where newlines placed ignored • #define & text in quotes major exceptions to rule • This can lead to interesting code choices

  17. Coding Style • Use consistent size to indent (I use 2 spaces) • Not required, but makes it much easier to read code • Indent code within braces ({}) to show structure • If line could or does contain { add to indentation • Decrease indentation each }should be on a line • if,else if, or elseline has opening brace • Always use braces to prevent having to fix bugs • Limit space, closing brace on else/else if line • Closing brace on own line otherwise (like at end)

  18. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  19. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  20. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  21. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  22. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  23. Coding Style Example #include <iostream>using namespace std;int main(void) {intdcScore = 27, hstScore = 30; if (dcScore > hstScore) {cout << "Prof. Hertz happy" << endl;cout << "His team won" << endl; } else {cout << "His team lost." << endl;cout << "He is sad";dcScore += 100;if (dcScore < hstScore) {cout << "Ouch. That's bad." <<endl;} } cout << "And now code continues" << endl; return 0;}

  24. Your Turn • Get in groups of 3 & work on following activity

  25. For Next Lecture • Read sections 7.4, 7.6 – 7.7 for Thursday • What if we want to run multiple clauses? • Is something easier for testing lots of equality? • How can we breaklots of things? • Week #3 weekly assignment due today • If problem takes more than 10 minutes, TALK TO ME! • Week #4 weekly assignment also on Angel

More Related