Download presentation
Presentation is loading. Please wait.
1
Repetition Structures (Loops)
Lesson - 7 Repetition Structures (Loops)
2
Introduction Any computer program is executed sequentially if the programmer doesn't change the flow of the program. The sequential programs start from the first line of the program code and execute all the statements one by one until the end of the program. There are two kinds of program flow controls: Decision Structures Repetition Structures
3
Decision Structures Decision Structures are used to alter the normal flow of program execution based on an evaluation of one or more logical expressions (condition). We have already studied decision structures in the previous lessons.
4
Repetition Structures
Repetition Structures are used to repeat a block of code either a specific number of times or while some condition remains true. For example: "Read 50 items from the keyboard" or "While Esc isn’t pressed continue reading from keyboard."
5
Repetition Structures Types
There are three kinds of repetition structures in C++: While Do While For
6
The "while" Loop The “While” Loop is the easiest repetition structure to understand for beginners. Its structure suits the most to the description stated above. The structure is made up of a logical condition and the block of code to be repeated.
7
The "while" Loop First, the condition is evaluated, if it is true the statements in the block of the code are executed, and the condition is evaluated one more time. This process continues until the condition becomes false. The condition must become false at the end; otherwise we can fall into an infinitive loop.
8
Example Program Let's make a basic program to understand the “While” Loop. Make a program which prints the numbers from 1 to 10. Program prints the value of a variable (that is a counter) one at a time, and repeats this process ten times. To print the numbers from 1 to 10, the variable counter is initialized to one, and its value is increased by 1 in each iteration. On the other hand, the condition "counter <= 10" checks if the counter exceeds its final value.
9
The key concepts of looping
The key concepts of looping are: Counter Initial Value of the counter Final Value of the counter Increment or Decrement Factor of the counter Condition and Statements to be executed. #include <iostream> using namespace std; int main() { int counter = 1; while (counter <= 10) cout <<counter<<" "; counter++; } system("PAUSE"); return 0;
10
Increment and Decrement Operators
C++ provides unary Increment (++) and Decrement (--) operators. Both operators can be used after (a++, post-increment) or before (++a, pre-increment) their operands. If the variable 'a' needs to be incremented by 1, "a++", "a=a+1", or "a+=1" can be used. int a=5; cout <<"initial a is "<<a<<endl; cout <<"after a++ is "<<a++<<endl; cout <<"current value of a is "<<a<<endl; cout <<"after ++a is "<<++a<<endl; cout <<"current value of a is "<<a << endl; cout <<"after --a is "<<--a<<endl;
11
Counter-Controlled and Sentinel-Controlled Repetitions
#include <iostream> using namespace std; int main() { int N, total, mark, counter; cout<<"How many marks?"; cin >> N; counter = 1; total = 0; while (counter <= N) cout <<"Enter the next mark [1..10]: "; cin >> mark; total+=mark; counter++; } float average = (float)total/N;. cout<<"The average is "<<average<<endl; system ("pause"); return 0; Counter-Controlled Repetition uses a variable called a "counter" to control the number of times the statements will be executed. The variable counter must be initialized before the repetition and must be incremented or decremented during the repetition process. Counter-Controlled Repetition is called “definite repetition " because the number of repetitions is known before the loop begins executing.
12
Sentinel-Controlled Repetition
In the case when users don't know the number of the repetitions in advance, a Sentinel-Controlled Repetition can be used instead of a counter-controlled repetition. The idea of a Sentinel-Controlled Repetition is that there is a special value (the "Sentinel”) that is used to say when the loop is done. The sentinel value must be chosen carefully so that it cannot be confused with a legitimate value. Sentinel-Controlled Repetition is called "indefinite repetition” because the number of repetitions is not known in advance.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.