G6DICP - Lecture 5 Control Structures
Loops A block of code is repeated A pre-defined number of times Until a certain (boolean) condition is met Determinate vs indeterminate The contents of variables often change in each cycle. Beware of perpetual loops!
For Loops Probably the most common type of loop. for ( starting condition; continuing condition; change each cycle ) for ( a=1; a<=10; a++ ) { ... }
While Loops while (continuing condition) while (a<=10) { ... }
Do.. while Loops do { ... } while (continuing condition) do { ... } while (a<=10)
Switch switch (answer) { case ‘y’ : { ... } case ‘n’ : { ... } default : { ... } } case ‘y’ : { ... }; break; case ‘n’ : { ... }; break; default : { ... }; break;