Repetition Control Statements in C++
Also know as looping statements, or loops. These statements execute the statement(s) repeatedly until condition remains true. Allows programmer to specify that a program shall repeat an action while some condition remains true. Types of repetition control statement includes
- while statement
- do while statement
- for statement
While Statement
It executes the statement(s) repeatedly until condition remains true (Iteration). Statements contained in the while constitutes body of the while statement which can be a single statement or a block. As condition becomes false, loop is terminated and control is transferred to the statement after the repetition statement executes
Syntax:
initialization;
while (loopContinuationCondition)
{
Statement;
Increment;
}
Example:
int product = 3;
while ( product <= 100 ) {
product = 3 * product;
}
Assignment Exercise:
A class of ten students take a quiz. The grades ( integers in the range of 0 to 100) for this quiz are available to you. Calculates and display the total of all student grades and the class average on the quiz.
Counter control Repetitionis a technique in which a variable called counter is used to control iterations in a loop – counter controlled repetition. Also know as definite repetition. Iterations are known before loop is executed. Counter-controlled repetition requires:
- Name of the control variable (loop counter)
- Initial value of the control variable
- Loop continuation condition - Test perform in every iteration whether to continue loop or not
- Increment or decrement by which control variable is modified each time through loop
C++ Example
int main(void)
{
int counter = 1; // Declare and initialize loop variable
while (counter {
cout< counter++; // Incremental control variable by 1
} // End of while
getch();
return 0;
}
Another Example
int main(void)
{
int counter = 0; // Declare and initialize loop variable
while (++counter <=10) { // Loop continuation condition &
// Increment
cout<<counter<<" ";
}
getch();
return 0;
}
For Statement
Another type of loop statement – for loop. It specifies the counter control repetition details in a single line of code. When the for statement begins execution, the control variable is declared and initialized. Then the loop – continuation condition is checked and if condition is true, body executes. At the end counter is incremented or decremented.
Syntax:
Example:
int main(void)
{
for (int counter = 1; counter <=10; counter++ ){
cout<<counter<<" ";
}
getch();
return 0;
}
Few Examples Using “for” statement
for ( int i = 1; i <=100; i++ )
2. Vary the control variable from 100 down to 1 in the increment of -1
for ( int i = 100; i >=1; i-- )
for ( int i = 7; i <=77; i+=7 )
for ( int i = 20; i >=2; i -=2 )
for ( int i = 2; i <=20; i += 3 )
for ( int i = 99; i >=0; i -= 11 )
do…while Statement
This control statement is similar to the while statement. In the while statement, the loop continuation condition is checked at the beginning of the loop before the body of loop executed. The do…while statement tests the loop-continuation condition after the loop body executes. Therefore the loop body always executes at least once.
When the do…while terminates, execution continues with the statement after the while clause. Note that it is not necessary to use braces in the do…while if there is only one statement in the body. However most programmer include the braces to avoid confusion between the while and do…while statements.
We can use a do…while statement to implement the data-validation loop - a loop that continues to prompt and read data until valid data item is entered.
Syntax:
initialization;
do
{
Statement;
Increment;
}
while (loopContinuationCondition);
Example:
int main(void)
{
int counter = 1;
do
{
cout<<counter<<" ";
counter++;
}
while (counter <=10);
getch();
return 0;
}
Review of repetition control statements
while loop
- Mostly used when repetition is not counter controlled
- Condition test precedes each loop repetition
- Loop body may not be executed at all
- Used to implement a counting loop
- Also convenient for other loops with simple initialization and update steps
- Condition test precedes the execution of the loop body
- Convenient when at least one repetition of loop body is needed
Related Posts
- Queue Implementation in C++
- Binary Search Trees and Data Structure
- Bucket Sort (Bin Sort) with Example
- Quick Sort Algorithm with Graphical Explanation
- Heap Sort with Graphical Explanation
- Merge Sort with Graphical explanation
- Graphical Path Finding System using Dijkstra Algorithm
- Applications of Dijkstra Algorithm
- The OpenGL Programming Guide
- Introduction to Recursion in C++
Popular Posts (last 30 days)
- Attendance Management System 1534 view(s)
- Advanced Java Tutorial (For Intermediate) 801 view(s)
- JAVA Graphical User Interface (GUI) 784 view(s)
- Graph Implementation in C++ 548 view(s)
- File Handling using Input-Output Streams in Java 527 view(s)
- Linked lists in C++ 512 view(s)
- Sockets and Network Programming in Java 433 view(s)
- UDP Datagram Sockets in Java 426 view(s)
- Applications of Stack in data structures 422 view(s)
- Circular Linked Lists 379 view(s)







