// Lab 7 Lecture // comment the examples and run individually or several at once // by commenting out the 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() { // ----------------------------- While Loops ---------------------------------- // Here is an example of a simple WHILE Loop // while ( ) // { // // statements // } // Like a FOR loop the while loop will only execute if the conditional // statement is true, and will loop back up and continue to execute // over and over until the condition is false. // notice that the following is impossible to tell how many times it will // execute. It will depend on what the user inputs. char ans = 'y'; cout << "Entering WHILE Loop (enter 'n' to quit loop)" << endl; while (ans != 'n') { cout << "Bears, beets, Battlestar Galactica." << endl; cin >> ans; } // ----------------------------- Do-While Loops ------------------------------- // Here is an example of a simple DO-WHILE Loop // do{ // // code // } WHILE ( ); char ans2 = 'y'; cout << "Entering DO-WHILE Loop (enter 'n' to quit loop)" << endl; do{ cout << "Bears beets Battlestar Galactica." << endl; cin >> ans2; }while (ans2 != 'n'); // ------------------------- While Vs. Do-While Loops ------------------------- // There is a very subtle difference between a WHILE and a // DO-WHILE loop. A WHILE loop tests it's condition before // entering the loop, whereas a DO-WHILE loop will test after the // code inside the loop has executed. This means that a DO-WHILE // loop will always run its code at least once, whereas a WHILE loop // may never run it's code. This can be demonstrated via the following. cout << "WHILE loop Vs. DO-WHILE loop" << endl; //while while(false){ cout << "WHILE LOOP WIN" << endl; } //do-while do{ cout << "DO-WHILE LOOP WIN" << endl; }while(false); // ----------------------------- Infinite Loops ------------------------------- // one thing you must always be careful when dealing with loops is the possibility // of an infinite loop. Why is this an infinite loop? (uncomment before you run) // int choice, i; // cout << endl << "Enter a positive number." << endl; // cin >> choice; // // i = 0; // while (i < choice){ // cout << "In infinite loop" << endl; // } // ------------------------------ Nested Loops -------------------------------- // While and do-while loops can be nested. How many times will the 'cout' // execute? This is also how to implement a while loop to iterate a specific // number of times. const int SIZE2 = 3; int k; int j; int count_1 = 0; cout << "\nEntering WHILE Loop 3" << endl; k = 0; while (k < SIZE2){ j = 0; while (j < SIZE2){ count_1++; cout << "k : " << k << " j : " << j << " count : " << count_1 << endl; j++; } k++; } cout << endl; // Would a FOR loop be better suited for this task? // ------------------------- Do-While Loop Example ---------------------------- // This program finds the factorial of any number below 20. // The program will loop until it is told to terminate. int selection, g; long long int total; // can hold very large positive integers char menu; do{ cout << "Welcome to my factorial calculator!" << endl; cout << "Please enter a positive number to find it's factorial!" << endl; cin >> selection; if (!selection) { // if selection is equal to 0 return true (!0 = 1) cout << "0! = 1" << endl; } else if (selection > 20) { cout << "The number entered is too large." << endl; } else if(selection > 0) { g = 2; total = 1; while (g <= selection){ total *= g; g++; } cout << selection << "! = " << total << endl; } else { cout << selection << "! = undefined" << endl; } cout << "Would you like to calculate another number? ('y' or 'n')" << endl; cin >> menu; }while(menu != 'n'); cout << "PROGRAM TERMINATED" << endl; return 0; }