// Lab 6 Lecture // comment the examples and run individually or several at once // by commenting out the indicated sections of code. // (Eclipse: ctrl+/ to comment and uncomment) // (Code Blocks: ctrl+shift+C to comment and ctrl+shift+x to uncomment) #include using namespace std; int main() { // --------------------------- For Loops -------------------------------------- // Here is a basic FOR Loop // // for ( ; ; ) // { // // Statements inside the loop // } // Like an IF statement a FOR loop will only execute the code inside of it if // its condition is true. However, unlike an if statement, once reaching the // closing brace the FOR loop will jump back up to the top, increment/decrement // the index, and test its condition again. As the name describes, this ends up // forming a loop that will repeat until the condition is false. // How many times will this run? 4? 5? 6? try it. const int SIZE = 5; cout << "Entering FOR Loop 1" << endl; for(int i = 0; i < SIZE; i++) { cout << "value of 'i' : " << i << endl; } // What if our conditional statement was 'i <= SIZE'? 4? 5? 6? // --------------- Basic For Loop to Iterate 'n' Times ------------------------ // As you may have figured out from the above example you // can always use this formula to create a FOR loop that will // loop 'n' number of times. int n = 3; for(int i = 0; i < n; i++) { cout << "I will print " << n << " times" << endl; } // ------------------------ Infinite Loops ------------------------------------ // one thing you must always be careful when dealing with loops is // the possibility of an infinite loop. Entering a number less than // 10 will cause an infinite loop. // Why? int choice; cout << "\nEntering a number less than 10 will cause an infinite loop." << endl << "Try it out! press the red box at the top of the console to kill the program." << endl; cin >> choice; for (int i = 10; i < choice; i--) { cout << "In loop 2" << endl; } // ---------------------- How For Loop Fields Work ---------------------------- // Yes this will actually run! Go ahead and run it. The first and third // field will take any single valid statement. // This loop will execute 5 times before exiting. // Does this shed some light on how the fields in a FOR loop work? int x = 0; for(cout << "First field\n" ; x < 5 ; cout << " Third field\n") { cout << " Body of Loop\n"; x++; } cout << endl; // ------------------------ Nested For Loops ---------------------------------- // FOR loops can be nested. How many times will the 'cout' execute? const int SIZE2 = 3; int count_1 = 0; cout << "\nEntering FOR Loop 3" << endl; for (int i = 0; i < SIZE2; i++) { for (int j = 0; j < SIZE2; j++) { count_1++; cout << "FOR loop 3 count : " << count_1 << endl; } } // -------------------- Variable Scope In For Loops --------------------------- // What is the scope of variables declared in a FOR Loop? // If the cout below the FOR loop is uncommented you will get an error // because 'i's scope is limited to the the FOR loop it was created in. // Also, could you declare 'count_2' inside a FOR loop and still get // the same output? Why or why not? int count_2 = 0; int SIZE3 = 3; cout << "\nEntering FOR Loop 4" << endl; for (int i = 0; i < SIZE3; i++) { for (int j = 0; j < SIZE3; j++) { count_2++; cout << "FOR loop 4 count : " << count_2 << endl; } } //cout << i << endl; cout << endl; // -------------------- For Loop Example ---------------------------- // Here is an example of two nested for loops used to create ascii art. // I declare 2 indexes, and increment/decrement them both. for (int start = 0, end = 15 ; start < end; start++, end--) { for (int i = 0; i < start; i++) { cout << " "; } for (int j = 0; j < end - start; j++) { cout << "*"; } cout << endl; } return 0; }