CS 107 - 6/29/15 for loops - alternative to the while loop. IN the while loop, we discussed 4 parts // intialization value = 1; while ( value <= 10 ) // condition { // body statements cout << value << endl; // increment value = value + 1; } The while is really controlled by the three parts of 1. the initialization 2. the condition 3. the increment However, they really can be all over the place. The for loop combines these three part together into on line of code. Syntax: for ( ; ; ) { ; } Rewrite the above while loop as a for loop for ( value = 1 ; value <= 10 ; value = value + 1 ) { cout << value << endl; } The portion of the for loop occurs AFTER the are executed and before the next is checked The for loop is covered in Chapter 7. Also covered in chapter 7 are switch statements and do-while loops The Switch statement is a alternative for the multiple condition if statement The switch is restricted to using equality as the condition and tends to work best with integers or characters. (Strings, floating point values tend not to work so well.) consider the following if statements int number; cin >> number; if ( number == 1 ) { // do this } else if ( number == 2 ) { // do that 1 } else if ( number == 3 ) { // do that 2 } else if ( number == 4 ) { // do that 3 } else if ( number == 5 ) { // do that 4 } else { // do some other thing } The above could be rewritten as a Switch statement cin >> number; switch ( number ) { case 1: // do this break; case 2: // do that 1 break; case 3: // do that 2 break; case 4: // do that 3 break; case 5: // do that 4 break; default: // do some other thing; break; } do-while statement is another form of the while loop The while loop checks the condition before the loop body is executed for the first time. The body of the while loop can be executed zero or more times The do-while loop checks the condition after the loop is execute for the first time the body of the do-while loop is executed one or more times Syntax of the do-while loop do { ; } while ( ) Jump to Chapter 11 in Dale book and discuss arrays. Standard variable store one value per variable name. Arrays allow multiple values (of the same type) to be stored under a single variable name. Array use a numeric/integer subscripts to keep track of which value is being accessed or modified. Creation of an array varaible: int arr1[10]; To store a value in an array, the following assignment statement would be used: arr1[5] = ; To access a value, the "square bracket" notation is again used to specify the value being accessed. cout << arr1[3]; int index = 7; value1 = arr[index] * 10; For an array of size X, the valid subscripts range from zero to X-1. Code to initialize all 10 positions/indices to 0: int arr1[10]; arr1[0] = 0; arr1[1] = 0; arr1[2] = 0; ... arr1[8] = 0; arr1[9] = 0; Or better yet, use a loop int i; for ( i = 0 ; i < 10 ; i++ ) { arr1[i] = 0; } When using arrays, we often will use a constant variable. The C style of create a constant is to use the #define directive #define ARRSIZE 10 // The array would be declared as: int arr2[ARRSIZE]; // The array values are initialized as: int i; for ( i = 0 ; i < ARRSIZE ; i++ ) { arr2[i] = 0; } The C++ style of creating a constant is to use the "const" keyword const int ARRSIZE = 10; // The array would be declared as: int arr2[ARRSIZE]; // The array values are initialized as: int i; for ( i = 0 ; i < ARRSIZE ; i++ ) { arr2[i] = 0; } Assignment 2 - Mastermind / Code Breaker https://en.wikipedia.org/wiki/Mastermind_%28board_game%29 If the code and the guess are each stored in an array int code[4]; int guess[4]; // to generate the code: int i; for ( i = 0 ; i < 4 ; i++ ) { code[i] = formula to generate a value from 1 to 6. } // to determine the number of correct codes in the // correct positions int numCorrect = 0; int i; for ( i = 0 ; i < 4 ; i++ ) { if ( code[i] == guess[i] ) numCorrect = numCorrect + 1; } cout << "The number of correct code values in their correct " << " positions are: " << numCorrect << endl; C libray of stdlib has a function called rand()