Loop Control Structures(Decision Making & Looping)
- Looping is a powerful programing technique through which a group of statements is executed repeatedly, until certain specified condition is satisfied.
- Looping is also called a repetitive or an iterative control mechanism.
- A loop in a program essentially consists of two parts, one is called the body of the loop and other is known as the control statement.
- The control statement performs a logical test whose result is either true or false. If the result of this logical test is true, then the statements contained in the body of the loop are executed. Otherwise, the loop is terminated.
- The control statement can be placed either before or after the body of the loop.
- If the control statement is placed before the body of the loop, it is called entry-controlled loop.
- If the control statement is written after the body of the loop, it is called the exit-controlled loop.
- If the logical test condition is carelessly designed, then there may be a possibility of formation of an infinite loop which keeps executing the statements over and over again.
- while-statement – This is used to execute a set of statements repeatedly as long as the specified condition is true.
- Referred to as pretest loop.
- Syntax -
while (logexp)
{
statement;
}
- do-while statement – This is used to execute a set of statements repeatedly until the logical test results in false.
- this is called the post-test loop. Because the test for repetition is made at the end of each pass.
- Syntax -
do
{
statement;
}
while (logexp);
- At least once, the body of do-while is executed.
- The body of do-while must contain either implicitely or explicitely statements to modify the variable involved in the logexp.
- for-statement – This statement is used when the programmer knows how many times a set of statements is executed.
- Syntax -
for (expression1;expression2;expression3)
{
statement;
}
- Nested-for statement – If there are many data items to be processed against a set of elements repeatedly, then a single for statement is not adequate. Then, we must use the nested-for loop.
- If one for statement is completely placed within the other, it is known as the nested-for statement.
- Syntax -
for(exp11;exp12;exp13)
{
for(exp21;exp22;exp23)
{
statement1;
statement2;
}
}
Jumps in loops -
- break statement – This is used to teminate a loop and exit from a particular switch case label.
- continue statement – This is used as the bypasser. The control does not come out of the loop, instead it skips the remaining statements within the body of that loop and transfers to the beginning of the loop.
Related posts:








Leave your response!