1: //if.cpp
2: //Illustrates an if-statement, an if...else-statement,
3: //a sequence of if-statements, and a nested if-statement.
5: #include <iostream>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program analyses a lecture section and a mark.\n\n";
12: int mark;
13: char lectureSection;
15: cout << "Enter A, B or C, followed by a mark in the range 0..100: ";
16: cin >> lectureSection >> mark; cin.ignore(80, '\n');
17: cout << endl << endl;
19: //An if...else-statement (control structure)
20: if (lectureSection=='A' || lectureSection=='B' || lectureSection=='C')
21: cout << "OK, your lecture section is " << lectureSection << ".\n";
22: else
23: cout << "That's not a valid lecture section.\n";
25: if (mark >= 50) //An if-statement (control structure)
26: {//if-statement body with more than one statement requires braces
27: cout << "\nYou have received a passing mark.\n";
28: cout << "And that was not an easy course!\n";
29: }
31: //Here is a sequential-if construct (sequence of if-statements):
32: if (mark >= 90) cout << "That is in fact a top-notch mark!\n";
33: if (mark >= 80) cout << "Congratulations on doing so well!\n";
34: if (mark >= 65) cout << "You may proceed to the next course.\n";
35: if (mark >= 50 && mark < 65)
36: cout << "You should probably not proceed.\n";
37: if (mark < 50) cout << "You may not proceed.\n\n";
39: //Here is a nested-if construct (nested if-statement):
40: if (mark >= 80)
41: cout << "Your letter grade is an A.\n";
42: else if (mark >= 70)
43: cout << "Your letter grade is a B.\n";
44: else if (mark >= 60)
45: cout << "Your letter grade is a C.\n";
46: else if (mark >= 50)
47: cout << "Your letter grade is a D.\n";
48: else
49: cout << "Your letter grade is an F.\n";
50: cout << endl;
51: }