1 / 20

If-Else Statement

If-Else Statement. Review. So far… Input Output Remember and Modify data Yet to come… Decision branching Looping. The if-else Statement. if (expression)     statement1 else     statement2.

mateja
Download Presentation

If-Else Statement

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. If-Else Statement

  2. Review • So far… • Input • Output • Remember and • Modify data • Yet to come… • Decision branching • Looping

  3. The if-else Statement if (expression)    statement1else    statement2 allows a program to take one of two branches or paths of execution depending on the value of some expression

  4. The if-else Statement if (expression)    statement1else    statement2 allows a program to take one of two branches or paths of execution depending on the value of some expression

  5. The if-else Statement if (expression)    statement1else    statement2 allows a program to take one of two branches or paths of execution depending on the value of some expression

  6. The if-else Statement if (expression)    statement1else    statement2 allows a program to take one of two branches or paths of execution depending on the value of some expression

  7. Warning! if (temperature > 100); {cout << “It’s hot outside”; }

  8. Example #1 if (4) cout << “hello”;

  9. Example #2 if (0) cout << “hello”;

  10. Example #3 cout << “enter radius: “; cin >> radius; if (radius <= 0) { cout << “Error: no circle has non-positive radius!” << endl; exit(1); }area = PI * radius * radius; cout << “The circle with radius ” << radius << “ has area ” << area << “.” << endl;cout << “Ending this example...” << endl;

  11. Example #3 (revisited) cout << “enter radius: “; cin >> radius; if (radius <= 0) cout << “Error: no circle has non-positive radius!” << endl;else{    area = PI * radius * radius; cout << “The circle with radius ” << radius << “ has area ” << area << “.” << endl;} cout << “Ending this example...” << endl;

  12. Tabs vs Spaces if (tabs == true) {cout << “This is”; cout << “ an example”; cout << “ of bad”; cout << “indentation.”;cout << endl;}

  13. const int MAX_A = 100;const int MIN_A = 90;const int MIN_B = 80; cout << “enter average: ”;cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else    if (ave >= MIN_A)        grade = ‘A’;    else        if (ave >= MIN_B)            grade = ‘B’;        else            if (ave >= MIN_C)                grade = ‘C’;            else                grade = ‘F’;

  14. const int MAX_A = 100;const int MIN_A = 90;const int MIN_B = 80; cout << “enter average: ”;cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else    if (ave >= MIN_A)        grade = ‘A’;    else        if (ave >= MIN_B)            grade = ‘B’;        else            if (ave >= MIN_C)                grade = ‘C’;            else                grade = ‘F’;

  15. const int MAX_A = 100;const int MIN_A = 90;const int MIN_B = 80; cout << “enter average: ”;cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else    if (ave >= MIN_A)        grade = ‘A’;    else        if (ave >= MIN_B)            grade = ‘B’;        else            if (ave >= MIN_C)                grade = ‘C’;            else                grade = ‘F’;

  16. const int MAX_A = 100;const int MIN_A = 90;const int MIN_B = 80; cout << “enter average: ”;cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else    if (ave >= MIN_A)        grade = ‘A’;    else        if (ave >= MIN_B)            grade = ‘B’;        else            if (ave >= MIN_C)                grade = ‘C’;            else                grade = ‘F’;

  17. const int MAX_A = 100;const int MIN_A = 90;const int MIN_B = 80; cout << “enter average: ”;cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else    if (ave >= MIN_A)        grade = ‘A’;    else        if (ave >= MIN_B)            grade = ‘B’;        else            if (ave >= MIN_C)                grade = ‘C’;            else                grade = ‘F’;

  18. cin >> ave;if (ave > MAX_A)cout << “Error: average out of range!” << endl;else if (ave >= MIN_A)    grade = ‘A’;else if (ave >= MIN_B)    grade = ‘B’;else if (ave >= MIN_C)    grade = ‘C’;else if (ave >= MIN_D)    grade = ‘D’;else if (ave >= 0)    grade = ‘F’;else    exit(3);

  19. The exit Function return 0; exit (3);

  20. End of Session

More Related