Lecture 07-2 Programming in C++ PART 2 By Assistant Professor Dr. Ali Kattan 1
The while Loop and do..while loop In the previous lecture we studied the for Loop in C++. In this lecture we will cover iteration using while and do..while loops. Remember, iteration means repeating some code until some condition terminate the loop. 2
The while statement The while statement in C++ has the following simple form: It means in English: while the condition is true, do the statement. The loop will stop only when the condition is false. Notice that if the condition is not true, the loop will never run. If you have more than one statement, then use curly braces: { } 3
The while loop: Simple example The following is a simple while loop example to print numbers from 1 to 5. The while statement checks the condition BEFORE going inside the body of the loop. 4
The while loop: Simple example Notice the flowchart for while. The condition check is at the beginning before loop statements. 5
The while loop: More useful example The following program is used to sum positive numbers. It will keep asking the user to enter a new number until the user enters a negative number to stop the program. 6
The while loop: More useful example 2 The following program is used print powers of 10 (10, 100, 1000..) Notice the line power *=10; You can replace this line with power = power + 10; However, it is shorter to type the one in the program. 7
The while loop: More useful example 3 In the following program, the while condition is actually a cin input statement! You should know that if cin successfully enters an integer then this is regarded as true, otherwise false. 8
The while loop: More useful example 3 Notice that if you keep entering integer numbers the while condition will be true. If you enter a character (like Q), program stops. 9
The while loop: More useful example 4 In the following program, the while condition is a check for a Boolean value ok. As long as ok is true, the loop will continue. The program counts and ask the user each time. 10
The while loop: More useful example 5 The following program is similar to the previous but it checks for the (not) condition. 11
The do..while statement The do..while statement in C++ has the following simple form: It means in English: do the statement, then check the condition and if true do the statement again. The loop will stop only when the condition is false. Notice that the loop will run at least once even if the condition is false. 12
The do..while statement If you have more than one statement, then use curly braces: { } Basically, do..while is similar to while but the condition is checked at the end of the loop, not at the beginning. Because of this, the loop body in do..while runs at least once and this is unlike while loop since the loop body may never run. 13
The while and do..while statement See the difference between the flowchart for while and that for the do..while statement. In while, the condition check is at the beginning. In do..while, the condition check is at the end. 14
The do..while loop: Simple example The do..while statement checks the condition AFTER going inside the body of the loop. Try this: compare this program with the code in slide 4. Change line 5 so that k = 6 and see the difference in output. 15
Compare do..while & while loop How many times the word hello will be printed by each program? If we change p = 0 in line 3, how many times the word hello will be printed by each program? 16
Homework (for LAB) Problem 1: Write a program using while loop to print the numbers that can be divided by 3 or 7 starting from 40 down to 5. Problem 2: Same program in problem 1 but using do..while loop. Problem 3: Same program in problem 1 but using for loop. 17
Remember: If you don t practice C++ programming on the computer, YOU WILL NOT LEARN anything. To become a professional programmer you must try all the examples by yourself. 18
Thank You My Office, 3 rd floor, 303 Email: ali.kattan@nobleinst.com Website: www.alikattan.org 19