Download presentation
Presentation is loading. Please wait.
1
Iteration CSCE 121 J. Michael Moore
2
Logical Parts of a Loop and Terminology
Loop Body Don’t forget to initialize Iteration One instance of executing the loop body. Termination Condition (boolean expression) AKA loop control, exit condition, looping variable, etc. Initialize Update
3
Creating a Loop What do things have to look like before entering the loop? Initialization (control variable) Initialization (loop body) How do you get out of the loop? Control variable Termination condition What do you do ensure you can exit the loop? Update What does the loop do? Loop Body
4
While // initialize while (termination condition) { // do stuff // update } Note: Curly braces are optional if there is a single statement.
5
For Note: Curly braces are optional if there is a single statement. // A. initialize // B. termination condition // C. update for(A; B; C) { // do stuff } Do NOT update control variable inside loop.
6
Types of control Counting Sentinel Flag
Control variable increments by set amount each iteration. Sentinel Control variable is set to a value obtained during the loop each iteration. Flag Control variable is a boolean variable that represents a condition each iteration.
7
Do While // initialize do { // do stuff // update (can be initialization) } while (termination condition); Note: Curly braces are optional if there is a single statement.
8
Loops Loop type Minimum times through loop Maximum times through loop
While For Do While 1 Loop type Initialization Update While Outside of loop Within loop body For Built into statement Do While
9
Loop conversion Most loops can be converted to any other type of loop.
What can’t? (Well you could but it would be bad practice) Good exercise that can help you build good loops structures. Remember the guidance for building a loop…
10
Teaser New (for C++) loop in C++ 11
Range based for loop (we’ll talk more after we do vectors).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.