/* This program demonstrates * 1. While loops * 2. do-while loops * 3. Looping on ranges * 4. Counting loops * 5. For loops * * Loops: * Loops are repetitive statements. They execute the same lines of code * over an over again, as long as a certain condition is true. * The first type of loop is the while loop. "while" is a keyword. * A loop has 4 parts: initialization, condition, loop body and increment. * * while loops * while loops are the first type of loop. It checks the condition first. * If true, the loop body is executed. If false, it skips over the loop body * and goes to the line after the loop. * * do - while loop * The do-while loop is the second kind of loop * this loop checks the condition AFTER executing the loop body, when the * while loop checks the condition BEFORE executing the loop body. * So, the do-while executes the loop body AT LEAST ONCE. * Otherwise, the while loop and the do-while can be written in terms of the other * * for loop * The for loop is the third kind of loop available in C++ * It is used primarily as a counting loop. * That is, we like while loops when we don't know how many times a loop would run. * We like do-while loops when we have a situation similar to the while loop, * but the loop would run at least once. * We like for loops when we know exactly how many times the loop would run. * The syntax for the for loop is: * for ( initialization; condition; increment) * { * body of the loop * } * Control Flow: the initialization part is executed first, and only once. * Then the condition is checked. If the condition were false, it skips over * the loop and moves on to the line after the loop. If the condition were * true, it enters the loop and executes the statements in the body of the * loop. * Once we are done with the loop body, the increment part is executed, and * then the condition is checked again. This process is repeated until the * condition is false. * A few more things to note: * 1. We can have multiple comma separated statements in the initialization * and the increment part, but only one standalone condition. * 2. Any variables declared as a part of the for loop are not available * outside the loop. * * Stepwise Refinement with loops * Loops can be used to solve problems inside-out and piecemeal. * If a problem involves repeating the same process for a series of inputs/values * then we can solve the problem once, and wrap a loop around the solution after * adjusting the value to change every time the loop iterates. */ #include using namespace std; int main() { /* Print the first 200 natural numbers 1 - 200 * Step 1: Start at 1 * Step 2: Check if we're done/not done. If done, go to step 4 * Step 3: Print the number * Move to next number - increment * Go back to step 2 * Step 4: Move on */ cout<<"Before the while loop"<>low>>high; cout<<"While loop: \n"; int i = low; while (i <= high) { cout<>numEntries; i = 0; // i -> iteration. Number of times the loop has run so far while (i < numEntries) { cout<<"Enter a number: "; cin>>curVal; // Read in a number to examine if(curVal % 2 == 0) { countEven++; // we found one more even number } i++; // loop has run one more time } cout<<"Number of even entries: "<< countEven<