/* This program demonstrates * 1. Counting down and accumulation * 2. Counting loops * 3. for loop * 4. break and continue * 5. overflow and underflow * 6. variable scope * 7. Looping on a sentinel * * Accumulation * 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. * * The 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. * * break and continue statements in the context of loops * We took a look at the break statement when we discussed the switch * control structure. When used in a switch statement, the break statement * broke program control out of the case and took it to the end of the switch. * When used in a loop, it will take program control out of the loop, even * if the loop condition is still true. * 1. It doesn't matter if the break is in a big complicated if...else * statement in the loop. It will still end the loop. * 2. If the break is inside a nested loop, it will only exit the * immediately enclosing loop. The outer loops will still exectue normally. * 3. If the break is a part of a switch statement inside the loop, the loop * is unaffected. * BREAK STATEMENTS ARE CONSIDERED POTENTIAL SECURITY HOLES. THERE ARE VERY * FEW SITUATIONS WHERE BREAKING OUT OF A LOOP CANNOT BE REWORKED INTO A MODIFIED * LOOP CONDITION. * IF YOUR HOMEWORK PROGRAMS USE BREAK ANYWHERE EXCEPT IN A SWITCH STATEMENT, * YOU WILL BE DOCKED 10 POINTS FOR "BAD PROGRAMMING PRACTICE" * * The continue statement, on the other hand, will ignore the rest of the lines * in the loop body for this current iteration of the loop and move on to the * next iteration. (Iteration is defined as one run of the loop - condition -> * loop body -> increment). * If a continue statement is in a for loop, it will take control to the increment * In a while or do-while loop, it will take it to the condition * CONTINUE STATEMENTS ARE ALSO CONSIDERED POTENTIAL SECURITY HOLES. THERE ARE * SITUATIONS THAT CALL FOR ITS USE. BUT MOST OF THOSE ALGORITHMS ARE A BIT TOO * ADVANCED FOR THIS COURSE. SAME RULE AS BREAK - USING CONTINUE IN YOUR HOMEWORKS * WOULD RESULT IN A LOSS OF 10 POINTS. * * Scope with respect to for loops * We can declare variables in the initialization part of the for loop, and * these variables are only available or "alive" inside the loop * * Looping on a sentinel * There are several instances where we have to do some processing several * times, but we no not always know how many entries we have. So, the programmer * and the user can agree on a pre-determined "sentinel" value. We read in the first * entry, and as long as the entry is not the sentinel, we do the processing and * grab the next entry. */ #include using namespace std; int i = 45; // global - don't do this int main() { /* Accumulation * "collecting" a series of values into 1 variable - build off update * Problem Statement - Calculate the cumulative class participation grade over 4 weeks * Step 1: Initialize a sum variable to 0, and a counter to 4. * Also declare a weekly grade variable. * Step 2: We're counting down here, so check if the number of weeks * left (counter) is over 0 * Step 2.a: Read the weekly value * Step 2.b: Add value to the sum * Step 2.c: Decrement the counter * Step 2.d: Go back to step 2 * Step 3: Print the total */ double weekly, total=0; int numWeeks = 4; while ( numWeeks > 0) { cout<<"Enter the weekly grade:"; cin>>weekly; total = total + weekly; // accumulation numWeeks--; } cout<<"Total grade: "<>low>>high; cout<<"While loop: \n"; int i=low; while( i<=high) { cout<>num; /* The following loop will stop executing the very first time i is a * multiple of the number entered by teh user */ cout<<"break - example"<0; s++) { cout << s << endl; } cout<<"After loop, s: "<0 ; k++ ) { cout<(k)< 0; i++ , j--) { cout<<"i is "<>inBank; cout<<"Enter transaction amount: "; cin>>amount; while(amount !=0) // 0 is sentinel { inBank += amount; // add amount to current balance // a negative amoutn for a withdrawal will auto-subtract cout<<"Enter next transaction: "; cin>> amount; } cout<<"Current balance is now $ "<