Presentation is loading. Please wait.

Presentation is loading. Please wait.

Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);

Similar presentations


Presentation on theme: "Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);"— Presentation transcript:

1 do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);

2 Things to Note This is a posttest loop – the continue condition is at the end of the loop body The loop body will ALWAYS be executed at least once even if the continue condition is false when the loop is entered.

3 Increment and Decrement Operators ++ and -- are called Increment and Decrement Operators They increase or decrease the operand by 1. If the operator is to the right of the operand, the current value of the operand is used AND THEN the operand is incremented or decremented. int i = 2; cout << i++; //outputs 2 and then increments I cout << i; //outputs 3 If the operator is to the left of the operand, the operand is incremented or decremented BEFORE it is used. int j = 5; cout << ++j; //ouputs 6 as j is incremented BEFORE it is displayed cout << j; //still ouputs 6 In situations where it makes no difference in the outcome whether or not you use post or pre increment or decrement, use the pre increment as it is slightly more efficiently implemented.

4 Things to Note Increment & Decrement Operators Suggested use at this point in your careers is for loop counter increment/decrement ONLY The amount of increment/decrement is ALWAYS 1 Ideally suited for use with ‘for’ loops (coming your way soon)

5 ‘for’ Loop (Known number of iterations) Syntax: for (initialization; continue_test; count_update) statement; OR for (initialization; continue_test; update) { statement1; statement2; … } Pay attention to the indentation for required style. Note: Semicolons are required inside the () of the for loop. This differs from what you have typically seen before now.

6 Initialization The initialization expression sets the starting point for the count variable Although syntax allows use of float or double count variables, you MUST use an int type for this class (Cumulative increment or decrement of non integers can create real problems unless you really know what you are doing.) The initialization expression is evaluated ONLY ONCE when the loop is entered the first time. It is acceptable to BOTH declare and initialize the count variable within the initialization expression. Note: In this class this is the ONLY variable definition that does not need to be at the top of the function. In this class you may declare the count variable in the initialization expression ONLY IF the count variable is used ONLY with the for loop. Do NOT change the value of the count variable inside the body of the loop

7 Scope (A necessary digression) Variables have a property called ‘scope’. The scope of a variable is the part of the program where the variable is recognized. Commonly, variables are defined/declared at the top of a program or function. However, in very long programs or those with very distinct divisions, it may be justifiable to define a variable closer to where it is used. We will not use this feature in this class with one exception. You may define the counting variable inside a for loop if it appropriate and limited to that loop. NOTE: Any variable declared in the initialization statement of a for loop will only be recognized by THAT for loop. That is, the scope, of the variable is limited to the body and conditions in that for loop.

8 Continue_Test The continue_test evaluates to true or false The loop will continue as long as the continue_test evaluates to true and will exit the loop code when it becomes false The for loop is a pretest loop. If the continue_test is false when the loop is entered the code in the body of the loop will never be executed

9 Count_Update The count_update expression increments or decrements the count variable The increment or decrement amount can be any amount as required by the needs of the program When the increment or decrement is 1 you can use the increment/decrement operators. You can also use the combined assignment operators OR – you can use a simple assignment statement such as i = i + 1 The count_update expression is executed AFTER the execution of the loop body.

10 Example The example program computes the factorial of a number. Remember: 5! (5 factorial) means 5*4*3*2*1 We’ll look at the code and step through it manually. Stepping through your code manually is a good method to use to help debug problems.

11 Nested Loops It is often useful to have nested loops. They can be the same kind of loop (i.e. nested while or do.. while) or one type of loop may be contained in the body of another type. Nested for loops are very common for processing data into something called an array (coming your way soon). That is, a for loop that contains in its body another for loop.


Download ppt "Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);"

Similar presentations


Ads by Google