Introduction to Programming Summer Term 2015 Dr. Adrian Kacso, Univ. Siegen adriana.dkacsoa@duni-siegena.de Tel.: 0271/740-3966, Office: H-B 8406 State: May 6, 2015 Betriebssysteme / verteilte Systeme Introduction to Programming (1) i Introduction to Programming Summer Term 2015 5 More Statements Betriebssysteme / verteilte Systeme Introduction to Programming (1) 112
5 More Statements... Contents The switch statement (a multi-if ) Loops An example: prime numbers Assertions Betriebssysteme / verteilte Systeme Introduction to Programming (1) 113 5.1 The switch Statement A calculator multiple if statements char command; cin >> command; if ( command == + ) add(); else if ( command == - ) subtract(); else if ( command == * ) multiply(); else if ( command == / ) divide(); else cout << "unknown command\n ; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 114
5.1 The switch Statement... A better way: the switch statement char command; cin >> command; switch ( command ) { case + : add(); case - : subtract(); case * : multiply(); case / : divide(); default: cout << "unknown command\n"; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 115 5.1 The switch Statement... General form of a switch statement switch ( expression ) { // any C++ expression case constant1: // test for equality statement-list; case constant2: case constant3: statement-list; // leave the switch statement // (optional) default: statement-list; // handle everything else (optional) Betriebssysteme / verteilte Systeme Introduction to Programming (1) 116
5.1 The switch Statement... Quiz: What is the output? int number; cin >> number; switch ( number ) { case 0: cout << "zero\n"; case 1: case 2: cout << "one or two\n"; case 3: cout << "three\n"; default: cout << "not between zero and three\n"; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 117 5.1 The switch Statement... A style-guide for switch Use break (almost) always If you do not use break, add a comment to show that you really mean it: case 1: do_something(); // fall through case 2: do_something_additional(); // fall through case 3: do_what_is_needed_for_1_2_and_3(); Always use a default clause to handle unforeseen cases This helps debugging later on! Betriebssysteme / verteilte Systeme Introduction to Programming (1) 118
5.2 Loops Repeating things Often, we have to repeat a step of an algorithm several times In C++, this is achieved with loops: while loops do while loops for loops Each execution of the loop s statements (its body) is called an iteration Betriebssysteme / verteilte Systeme Introduction to Programming (1) 119 5.2 Loops... while loops Syntax: while ( expression ) statement ; Example: cout << "How many hello s?\n"; cin >> number; while ( number > 0 ) { cout << "Hello!\n"; number--; Expression is evaluated before the loop body is entered The body may thus not be entered (executed) at all Betriebssysteme / verteilte Systeme Introduction to Programming (1) 120
5.2 Loops... do while loops Syntax: do statement ; while ( expression ); Example: char c; do { cout << "Do it again (y/n)?\n"; cin >> c; while ( c!= n ); Expression is evaluated after the loop body has been executed The body will always be executed at least once Betriebssysteme / verteilte Systeme Introduction to Programming (1) 121 5.2 Loops... Correctness of while and do while The loop expression must eventually become false (0) if not, the loop runs forever! This may be non-trivial (e.g., with if/else statements) Often, we have simple, counting loops, e.g. do this for all integers from 0 to 9 do this 12 times Such a loop typically has some initialization for the counter, a condition, and a step statement (like increment or decrement) Betriebssysteme / verteilte Systeme Introduction to Programming (1) 122
5.2 Loops... for loops for loops prescribe initialization, loop condition, and step Syntax: for ( statement ; expression ; statement ) statement ; // initialization condition step body Example: for ( int i = 0; i < 10; i++ ) { cout << "i = " << i << endl; The variable i is called loop control variable or loop variable Betriebssysteme / verteilte Systeme Introduction to Programming (1) 123 5.2 Loops... for loops... for ( statement ; expression ; statement ) statement ; Some remarks: all statements may also be empty, e.g.: int i = 1; for (; i < 10; ) { cout << "i = " << i << endl; if (i < 5) { i += 1; else { i += 2; statements may be complex (bad style!): for (int i=0; i<10; cout << "i = " << i++ << endl); Betriebssysteme / verteilte Systeme Introduction to Programming (1) 124
5.2 Loops... Nested loops Loops may be inside loops: // int i, j; for ( int i = 0; i < 5; i++ ) { for ( int j = 0; j < 20; j++ ) { cout <<. ; cout << endl; Style: Always use { and for the loop body! Betriebssysteme / verteilte Systeme Introduction to Programming (1) 125 5.2 Loops... Special statements inside a loop body exit the whole loop (without testing the loop expression) continue; exit the loop body and continue with the next iteration Style Guide: avoid the use of break and especially of continue inside loop bodies! it is (often) easier to understand if you do the same with if / else or exceptions ( 10) however, break is nice to use in search loops Betriebssysteme / verteilte Systeme Introduction to Programming (1) 126
5.3 An Example: Prime Numbers // Print all primes <= 100, using naive algorithm. Author: Roland Wismï 1 ller 2 #include <iostream> using namespace std; int main() { //int number, factor; bool prime; cout << 2 << endl; for ( int number = 3; number < 100; number += 2) { prime = true; for (int factor = 3; factor < number; factor += 2) { if (number % factor == 0) { prime = false; if (!prime) continue; // Note: bad style! cout << number << endl; Betriebssysteme / verteilte Systeme Introduction to Programming (1) 127 5.3 An Example: Prime Numbers... (Animierte Folie) cout << 2 << endl; for (number = 3; number < 100; number += 2) { prime = true; for (factor = 3; factor < number; factor += 2) { if (number % factor == 0) { prime = false; if (!prime) continue; cout << number << endl; number: 3 factor: prime: output: 2 Betriebssysteme / verteilte Systeme Introduction to Programming (1) 128
5.4 Assertions Often, you know that some condition always must (or better: should!) hold at a certain place in your program Good practice: include runtime checks of these conditions A good way to do this is via assertions Use #include <assert.h> Then use assert(expression); to check the condition Example: assert(divisor!= 0); When the program reaches an assert() statement and the expression is false (0), the program halts with an error message Beware: do not use side effects, like assert(i++ < 10);! Betriebssysteme / verteilte Systeme Introduction to Programming (1) 129 5.5 Summary switch statement switch ( expression ) { // any C++ expression case constant1: // test for equality statement-list; case constant2: case constant3: statement-list; // leave the switch statement // (optional) default: statement-list; // handle everything else (optional) Betriebssysteme / verteilte Systeme Introduction to Programming (1) 130
5.5 Summary... Loops while ( expression ) statement ; loop body may not be executed at all do statement ; while ( expression ); loop body is executed at least once for ( statement ; expression ; statement ) statement ; counting loop, with counter initialization, loop condition, and step Betriebssysteme / verteilte Systeme Introduction to Programming (1) 131 5.6 Code example using loops int main( void) { int i,j, k; i=j=0; cout << endl << "----- WHILE () {... ------" << endl; while (j<10) ++j; i += j; PRINT2(i,j); i=j=0; while (j<10) i += ++j; PRINT2(i,j); cout << endl << "----- DO {... WHILE (); ----- " << endl; i=j=0; do { PRINT2(i,j); j += 2; i++; while (j<7); Betriebssysteme / verteilte Systeme Introduction to Programming (1) 131
5.6 Code example using loops i=0; cout << "----- FOR ( ; ; ){... :----- " << endl; for ( j=1; j<100; j++); PRINT2(i,j); for ( j=1; (i=j)<7; j++) { PRINT2(i,j); cout << endl << "TEST:" << endl; PRINT2(i,j); cout << "execute loop:"; for ( int i=0, j=100; j>1; i++) { j /= 25; PRINT2(i,j); cout << "outside the loop:"; PRINT2(i,j); Betriebssysteme / verteilte Systeme Introduction to Programming (1) 131