OVERVIEW/REVIEW OF C++ (PART TWO) ====================== The computer executes statements from top to bottom in a function (SEQUENCE), unless you use a SELECTION or REPETITION structure. if statements (SELECTION / DECISION / BRANCHING) ------------- if ( logical_expression ) { // use braces! // statements to execute if expression evaluates to true } if ( logical_expression ) { // statements to execute if expression evaluates to true } else { // statements to execute if expression evaluates to false } if ( logical_expression#1 ) { // statements to execute if logical expression #1 evaluates to true } else if ( logical_expression#2 ) { // statements to execute if logical expression #1 evaluates to false // AND logical expression #2 evaluates to true } else { // statements to execute if neither expression evaluates to true } if ( logical_expression#1 ) { // statements to execute if logical expression #1 evaluates to true } else if ( logical_expression#2 ) { // statements to execute if logical expression #1 evaluates to false // AND logical expression #2 evaluates to true } else if ( logical_expression#3 ) { // statements to execute if logical expressions #1 and #2 // BOTH evaluate to false // AND logical expression #3 evaluates to true } else { // statements to execute if none of the above expressions // evaluate to true } You can have as many else if statements as you want. The computer evalutes them from top to bottom. ONLY THE FIRST ONE THAT EVALUATES TO TRUE has its statements executed, then all the rest (including the else) are skipped. If NONE of the if or else if expressions evaluate to true, then the statements following the else are executed. The else is optional. In this case, if no expression evalutes to true, NOTHING happens. Example: Convert a numeric grade to a letter grade. 91 - 100 A 81 - 90 B 71 - 80 C 61 - 70 D 60 or lower F int grade ; // user enters value for grade if ( grade > 90 ) { cout << 'A' ; } else if ( grade > 80 ) { cout << 'B' ; } else if ( grade > 70 ) { cout << 'C' ; } else if ( grade > 60 ) { cout << 'D' ; } else { cout << 'F' ; } Equivalent code, using reverse conditions and reverse order: if ( grade <= 60 ) { cout << 'F' ; } else if ( grade <= 70 ) { cout << 'D' ; } else if ( grade <= 80 ) { cout << 'C' ; } else if ( grade <= 90 ) { cout << 'B' ; } else { cout << 'A' ; } Better code, with input validation built in: if ( ( grade < 0 ) || ( grade > 100 ) ) { cout << "Invalid grade: " << grade << endl ; } else if ( grade > 90 ) { cout << 'A' ; } else if ( grade > 80 ) { cout << 'B' ; } else if ( grade > 70 ) { cout << 'C' ; } else if ( grade > 60 ) { cout << 'D' ; } else { cout << 'F' ; } while loops (REPETITION / ITERATION / LOOPING ) ----------- Used to loop ZERO or MORE times. while ( logical_expression ) { // statements that get repeated as long as the logical expression // evaluates to true. } As soon as the logical expression evaluates to false, the statements are skipped and the loop stops looping (and sequence goes to the first statement after the right brace.) If the logical expression evaluates to false the FIRST time, the statements are skipped and nothing happens. Using a loop counter: Counter Controlled Loop For example, count to 10. int count = 1 ; // loop counter variable while ( count <= 10 ) // counter controlled loop { cout << count << endl ; count++ ; // add one to the count variable // same as: count = count + 1 ; // same as: count += 1 ; } Example #2, count down: int count = 10 ; while ( count > 0 ) { cout << "T minus " << count << endl ; count-- ; // subtract one from the count variable // same as: count = count - 1 ; // same as: count -= 1 ; } cout << "We have lift-off!" << endl ; Using a loop exit value: Sentinel Controlled Loop For example, compute the class average: int sum = 0 ; // sum of all grades int count = 0 ; // number of grades entered const int SENTINEL = 999 ; // loop end value // NOTE: The sentinel value must be outside the range of normal // data. If this is not possible, then you must use alternate // means (enter data as strings or use end-of-file signals). int number ; // single grade while ( true ) // seemingly infinite loop { cout << "Enter a grade from 0 to 100, 999 to stop: " ; cin >> number ; if ( number == SENTINEL ) { break ; // exit the loop } if ( ( number < 0 ) || ( number > 100 ) ) // input validation { cout << number << " is an invalid grade!" << endl ; continue ; // re-start the loop from the top } sum += number ; // same as: sum = sum + number ; count++ ; } if ( count > 0 ) // prevent division by zero { int average = sum / count ; cout << "Class average is " << average << " (from " << count << " grades)" << endl ; } else { cout << "No grades entered." << endl ; }