C++ PROGRAMMING: HANDLING REPETITIONS FOR
LOOP STATEMENT
Loop statements in C++ execute the certain block of the code or statement multiple times, mainly used to
reduce the length of the code by executing the same function multiple times, reduce the redundancy of the code.
C++ supports various types of loops like for loop, while loop, do-while loop, each has its own syntax,
advantages, and usage.
In the programming world, the loop is a control structure that is used when we want to execute a block of code,
multiple times. It usually continues to run until and unless some end condition is fulfilled.
After doing this module, you are expected to:
1. Identify the flow of control using for loop statement
2. Use the for loop statement to formulate loop statements
3. Create a program using the for loop statement to satisfy certain problem
INTRODUCTION
In Kafka’s Metamorphosis, salesman Gregor Samsa led a boring, repetitive life—until he woke up one rainy
morning to find himself transformed into a giant cockroach. It is human nature to get bored and to lose interest
in certain activities if these have been done repeatedly for a very long time. Of course, just because we’re bored
doesn’t mean that we will immediately transform into vermin. However, doing anything repetitively over a long
period of time does lead to fatigue and inattention on the part of the person.
Monotony and boredom over highly repetitive tasks usually cause either or both of the following: (a) the person
loses concentration and begins to commit careless mistakes, or (b) he quits and walks away from the job. One of
the greatest strengths of a computer is its ability to perform repetitive tasks continuously without getting bored
or tired. With computers, we only have to worry about our own fatigue.
But just exactly how are such repetitive tasks handled in computer programs? Using only sequential and
selection control structures, one way of accomplishing the repeated execution of a particular operation is by
typing the same code in the program over and over again. This is an extremely inefficient or even unworkable
way of doing it.
Fortunately, there are repetition control structures specifically meant to handle the repeated execution of
portions of a program. These control structures are called loops. With loops, a test condition for repetition is
already built-in, and a separate goto statement is no longer required. Each pass through the loop is called
an iteration.
DISCUSSION
Counter-Controlled Loops
A counter-controlled loop is a control structure that allows us to specify the repeated execution of a group of
program statements a definite number of times. A variable that serves as an index or counter is used to indicate
how many times an enclosed block of statements is to be executed repeatedly.
for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
specific number of times.
The syntax of a for loop in C++ is :
for ( init; condition; increment )
LOOP STATEMENT
Loop statements in C++ execute the certain block of the code or statement multiple times, mainly used to
reduce the length of the code by executing the same function multiple times, reduce the redundancy of the code.
C++ supports various types of loops like for loop, while loop, do-while loop, each has its own syntax,
advantages, and usage.
In the programming world, the loop is a control structure that is used when we want to execute a block of code,
multiple times. It usually continues to run until and unless some end condition is fulfilled.
After doing this module, you are expected to:
1. Identify the flow of control using for loop statement
2. Use the for loop statement to formulate loop statements
3. Create a program using the for loop statement to satisfy certain problem
INTRODUCTION
In Kafka’s Metamorphosis, salesman Gregor Samsa led a boring, repetitive life—until he woke up one rainy
morning to find himself transformed into a giant cockroach. It is human nature to get bored and to lose interest
in certain activities if these have been done repeatedly for a very long time. Of course, just because we’re bored
doesn’t mean that we will immediately transform into vermin. However, doing anything repetitively over a long
period of time does lead to fatigue and inattention on the part of the person.
Monotony and boredom over highly repetitive tasks usually cause either or both of the following: (a) the person
loses concentration and begins to commit careless mistakes, or (b) he quits and walks away from the job. One of
the greatest strengths of a computer is its ability to perform repetitive tasks continuously without getting bored
or tired. With computers, we only have to worry about our own fatigue.
But just exactly how are such repetitive tasks handled in computer programs? Using only sequential and
selection control structures, one way of accomplishing the repeated execution of a particular operation is by
typing the same code in the program over and over again. This is an extremely inefficient or even unworkable
way of doing it.
Fortunately, there are repetition control structures specifically meant to handle the repeated execution of
portions of a program. These control structures are called loops. With loops, a test condition for repetition is
already built-in, and a separate goto statement is no longer required. Each pass through the loop is called
an iteration.
DISCUSSION
Counter-Controlled Loops
A counter-controlled loop is a control structure that allows us to specify the repeated execution of a group of
program statements a definite number of times. A variable that serves as an index or counter is used to indicate
how many times an enclosed block of statements is to be executed repeatedly.
for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
specific number of times.
The syntax of a for loop in C++ is :
for ( init; condition; increment )