Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures."— Presentation transcript:

1 Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures

2 Today We Are Going To: Describe the three C++ statements used for repetition: for, while and do-while Describe the three C++ statements used for repetition: for, while and do-while Use break and continue statements to exit loops “abnormally” Use break and continue statements to exit loops “abnormally”

3 Topic Repeating Actions

4 The Structure of a Loop In many cases a program must repeat the same processing many times In many cases a program must repeat the same processing many times Example: You are given a stack of envelopes and you must run through the stack, putting a stamp on each envelope Example: You are given a stack of envelopes and you must run through the stack, putting a stamp on each envelope C++ offers three types of looping: C++ offers three types of looping: while while do-while do-while for for

5 Syntax of a while Statement while (condition) {statement1;statement2;}

6 Syntax of a do-while Statement do{statement1;statement2;} while (condition);

7 Syntax of a for Statement for (initial-action; termination condition; end-of-loop-action){statement1;statement2;}

8 Notes As with if statements, blocks help make the program more readable even if there is only one statement to be executed As with if statements, blocks help make the program more readable even if there is only one statement to be executed Condition: Must use parens Condition: Must use parens Condition: Boolean expression Condition: Boolean expression

9 Notes (cont.) Difference between while and do-while is that the do-while will execute the body of the loop at least once; the while may not Difference between while and do-while is that the do-while will execute the body of the loop at least once; the while may not for loops are almost always used to handle a predefined number of iterations: for loops are almost always used to handle a predefined number of iterations: initial action sets start value of the counter initial action sets start value of the counter termination condition determines the number of iterations termination condition determines the number of iterations end-of-loop-action steps through values end-of-loop-action steps through values

10 Notes (cont.) It is possible to transform loops from one form into another in almost every case It is possible to transform loops from one form into another in almost every case The one exception is that it may not be possible to transform a while loop into a do-while loop because the do-while will force execution at least once The one exception is that it may not be possible to transform a while loop into a do-while loop because the do-while will force execution at least once

11 Example #1 Write a program that uses a for loop to count from 1 to 10 Write a program that uses a for loop to count from 1 to 10 Note: Could have declared the control variable within the loop Note: Could have declared the control variable within the loop for (int i = 1; i <= 10; i++)

12 Example #2 Now modify the program so that it will display the even numbers up to 20 Now modify the program so that it will display the even numbers up to 20 Note: The multiplication inside the loop does not affect the value Note: The multiplication inside the loop does not affect the value We could do it by using the control value We could do it by using the control value

13 Example #3 For loops do not have to count by ones, nor do they have to run upward For loops do not have to count by ones, nor do they have to run upward Rewrite your previous example so that it prints evens from 20 down to 2 Rewrite your previous example so that it prints evens from 20 down to 2

14 Example #4 Re-write the previous example to use a do-while loop instead of a for loop Re-write the previous example to use a do-while loop instead of a for loop Worth Noting: Worth Noting: the three elements in the for declaration appear verbatim in the do-while loop, just in new places the three elements in the for declaration appear verbatim in the do-while loop, just in new places

15 Example #5 Now do it again with a while loop Now do it again with a while loop Worth Noting: Worth Noting: the simplicity of the conditions, and the fact that we know the loop will execute once, make it easy to go back and forth from while to do-while the simplicity of the conditions, and the fact that we know the loop will execute once, make it easy to go back and forth from while to do-while

16 Topic Nesting Loops

17 Example #6 Write a program that will print a triangle of numbers. Each row must start with 1 and go one step further until the last row, which must count up to tenWrite a program that will print a triangle of numbers. Each row must start with 1 and go one step further until the last row, which must count up to ten Worth Noting: Worth Noting: the condition for the second loop depends on the first the condition for the second loop depends on the first

18 Topic “Abnormal” Termination of Loops

19 “Abnormal” Termination Loops are expected to run through all of the statements in the block, but there are exceptions to the rule Loops are expected to run through all of the statements in the block, but there are exceptions to the rule “Abnormal” is a misnomer; sometimes that is the desired result “Abnormal” is a misnomer; sometimes that is the desired result continue : statement used to end a single iteration of a loop continue : statement used to end a single iteration of a loop break : terminates the loop altogether break : terminates the loop altogether

20 Example #7 Write a program that will ask the user to input an integer, and display the square root (type double) Write a program that will ask the user to input an integer, and display the square root (type double) negative entries are not valid and require a re-prompt (an abnormal condition) negative entries are not valid and require a re-prompt (an abnormal condition) entering 0 terminates the program (normal) entering 0 terminates the program (normal) Worth Noting: Worth Noting: used a while(true) construct used a while(true) construct

21 Today We Covered: The utilization of looping control structures The utilization of looping control structures The nesting of loops The nesting of loops Statements used to terminate loops “abnormally” Statements used to terminate loops “abnormally”


Download ppt "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures."

Similar presentations


Ads by Google