/* This program demonstrates * he 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. * * */ #include using namespace std; int main() { /* Looping on a user-determined range - all 3 loops */ int high, low; cout<<"Enter the range: "; cin>>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 "<