/* This program demonstrates * 1. While loops * 2. do-while loops * 3. Looping on invalid input * 4. Counting loops * 5. Counting down and accumulation * * 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 * * Looping on Error Input * This continues on from the 3 methods of validating user input in * conditionals.cpp. Even if we ask the user to enter the corrected * value, there is no guarantee that the user would follow instructions. * It would be better if we can loop until the user gives us a value that * we can work with. * * ccumulation * Most of the time, we have to accumulate an operation. * For example, we want to add a bunch of numbers. * Accumulation is the process of keeping a running result. * For example, we might need to add all the numbers the user enters, stopping * at the first negative number. However, we can only add 2 numbers at * a time. So, we add the first number to the sum, then we add the next * number to the sum and store the result back in the sum, and we repeat * this process until we have added in the last number. */ #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"<>grade; while ( grade< 0 || grade > 100 ) // invert the condition for a "valid" grade { cout<<"You entered an invalid grade. Please enter a grade between 0 and 100: "; cin>>grade; // next read - increment } if (grade >= 70) cout<<"Pass"<0) { cout<<"Enter the weekly grade: "; cin>>weekly; total = total + weekly; numWeeks--; } cout <<"The total is : "<