loops
DESCRIPTION
pre testing and post testing loops C++TRANSCRIPT
![Page 1: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/1.jpg)
By Nita Arora (KHMS) 1
Repetition(Looping Concept)
![Page 2: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/2.jpg)
By Nita Arora (KHMS) 2
After studying this chapter you will be able to:After studying this chapter you will be able to:
OOBJECTIVESBJECTIVES
Understand the basic components of a loop: initialization, control
expression, and update.
Understand and use pretest, post-test, and count-controlled loops.
Differentiate between event-controlled and counter-controlled loops.
Write loops in C++ using while, for, and do...while loops.
Understand the limitations and use of break and continue statements in
loops.
![Page 3: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/3.jpg)
By Nita Arora (KHMS) 3
Concept of a Loop
![Page 4: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/4.jpg)
By Nita Arora (KHMS) 4
The two most important features of a computer are:
• The ability to compare values to make decisions, and
• The ability to repeat a series of operations in a controlled way.
![Page 5: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/5.jpg)
By Nita Arora (KHMS) 5
Figure : The concept of a loop
![Page 6: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/6.jpg)
By Nita Arora (KHMS) 6
Pretest and Post-Test
Loops
![Page 7: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/7.jpg)
By Nita Arora (KHMS) 7
•Loops need to be coded in such a way as that they may be ended in a controlled manner.
•The decision making process make take place at either the ‘top’ of the loop or at the ‘bottom’.
•When the test takes place at the top of the loop, it is referred to as a ‘Pretest Loop’.
•When the test takes place at the bottom of the loop, it is then a ‘Post-test Loop’.
![Page 8: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/8.jpg)
By Nita Arora (KHMS) 8
Pretest Loop
In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is
executed; if it is false, the loop is terminated.
Post-test Loop
In each iteration, the loop action(s) are executed. Next, the loop control expression is tested. If it is
true, a new iteration is started; otherwise, the loop terminates.
Note:Note:
![Page 9: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/9.jpg)
By Nita Arora (KHMS) 9
Figure : Pretest and post-test loops
“WHILE” test at the top…. “WHILE” test at the bottom
CONDITION
CONDITION
![Page 10: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/10.jpg)
By Nita Arora (KHMS) 10
Figure : Two different strategies for starting exercise
CONDITION
CONDITION
False
True
![Page 11: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/11.jpg)
By Nita Arora (KHMS) 11
Figure : Minimum number of iterations in two loops
It is important that you understand this critical difference between the two basic types…
![Page 12: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/12.jpg)
By Nita Arora (KHMS) 12
Initialization and Updating
![Page 13: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/13.jpg)
By Nita Arora (KHMS) 13
Figure : Loop initialization and updating
The initialization may optionally be part of the loop structure.
![Page 14: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/14.jpg)
By Nita Arora (KHMS) 14
Figure : Initialization and updating for exercise
![Page 15: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/15.jpg)
By Nita Arora (KHMS) 15
Event-Controlled and
Counter-Controlled Loops
![Page 16: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/16.jpg)
By Nita Arora (KHMS) 16
Figure : Event-controlled loop concept
![Page 17: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/17.jpg)
By Nita Arora (KHMS) 17
Figure : Counter-controlled loop concept
![Page 18: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/18.jpg)
By Nita Arora (KHMS) 18
Loopsin C++
![Page 19: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/19.jpg)
By Nita Arora (KHMS) 19
Figure : C++ loop constructs
• All three C/C++ loop constructs involve a test.
• The only difference is when the test occurs – either before or after doing something..
![Page 20: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/20.jpg)
By Nita Arora (KHMS) 20
Figure : The while statement
while the expression is true, do …..
![Page 21: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/21.jpg)
By Nita Arora (KHMS) 21
Figure : Compound while statement
![Page 22: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/22.jpg)
By Nita Arora (KHMS) 22
Figure : for statement
The for is just a special case of a while loop.
![Page 23: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/23.jpg)
By Nita Arora (KHMS) 23
The for loop is constructed in this way…
for ( initialization statement(s),
while test expression,
update statement(s) )
{
body of loop…
}
![Page 24: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/24.jpg)
By Nita Arora (KHMS) 24
A for loop is used when your loop is to be executed a known number of times.
You can do the same thing with a while loop, but the for loop is easier to
read and more natural for counting loops.
Note:Note:
![Page 25: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/25.jpg)
By Nita Arora (KHMS) 25
Figure : Compound for statement
![Page 26: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/26.jpg)
By Nita Arora (KHMS) 26
Figure : Comparing for and while loops
![Page 27: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/27.jpg)
By Nita Arora (KHMS) 27
In C/C++ the for loop counter is almost always initialized to a ZERO. So, if you want to do something 5 times, the for loop would be coded like this… for (int i = 0; i < 5; i++) {
do something… }It is also legal to have the test expression limit be a variable, as in: for (int i = 0; i < Num; i++)
![Page 28: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/28.jpg)
By Nita Arora (KHMS) 28
Figure : Format of the do…while statement
![Page 29: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/29.jpg)
By Nita Arora (KHMS) 29
Pre-test and post-test loops
![Page 30: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/30.jpg)
By Nita Arora (KHMS) 30
Assignment
1. Write a program (WAP) in C++ to accept a number and print its table upto 10 terms.
2. WAP in C++ to print the following output using for loops:1223334444
3. WAP in C++ to accept a number and calculate its factorial.eg. If number = 5
Factorial of 5 = 120 should be displayed
![Page 31: Loops](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555cdcdfd8b42aeb2c8b53fe/html5/thumbnails/31.jpg)
By Nita Arora (KHMS) 31
The End