/* This program demonstrates * for loops with multiple initializations, conditions and increments * The break statement * The continue statement * Nested Loops * Variable Scope * * Whne we use a for loopm we need to be conscious of a few points * 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. * * Nested loops * We can place loops inside loops. If we do so, the inner loop runs several times * for EVERY iteration of the outer loop. We can also use nested loops in * conjunction with selection statements in any * combination. * * Variable Scope * The scope of a variable is the amount of time (or the number of lines) where a * variable is considered to be "active" or "alive". A variable's scope extends from * the line in which it is declared, until the next enclosing '}' * * WE CAN HAVE MANY VARIABLES OF THE SAME NAME AS LONG AS THEY ARE IN DIFFERENT SCOPES * * When we make a reference to a variable name, as a part of a cout statement, expression, * assignment, condition, etc, C++ will always resolve this to the most recent variable * of that name that is still "alive" * * 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 */ #include using namespace std; int i = 45; // global - don't do this int main() { /* The for loop can take multiple comma separated values in the initialiation * and increment parts, as shown below. * We can even leave out those parts. * And, while multiple comma separated conditions in the loop condition part * is syntactically ok, ONLY ONE CONDITION WOULD BE CONSIDERED AND WHICH ONE * DEPENDS ON THE COMPILER. THIS WOULD GIVE YOU LOGICAL ERRORS AND UNEXPECTED * RESULTS. PLEASE AVOID * Ideally, only one eventual condition - if there are multiple conditions, they * should be combined with &&, || or ! into one condition. * HAVING MORE THAN 2 SEMICOLONS IN A FOR LOOP WOULD RESULT IN A COMPILATION ERROR */ for(int i= 0, j = 5; i<25 || j > 0; i++ , j--) { cout<<"i is "<>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"<