How to Use Loop Control Structures  in C++

How to Use Loop Control Structures in C++

In programming languages – while coding a program, sometimes it is necessary to repeat a set of statements several times (LoopingControl Structures ). A way to repeat statements is to type the same statements in the program over and over. For example, if you want to repeat some statements 100 times, you type the same statements 100 times in the program. However, this solution of repeating statements is impractical, if not impossible. Fortunately, there is a better way to repeat a set of statements.

As noted earlier, C++ has three repetition, or looping control structures that allow you to repeat a set of statements until certain conditions are met.

Note: The variable that controls the loop is called loop control variable (LCV)

See Also: 

While Loop Control Structure in Programming

The first loop that we’re going to discuss about is while loop. The general form of while loop is:

[codesyntax lang=”c”]

[/codesyntax]

In C++, while is a reserved word. Of course, the statement can be either a simple or compound statement. The expression acts as a decision maker and is usually a logical expression. The statement is called the body of the loop. Note that the parentheses around the expression are part of the syntax.

while

[codesyntax lang=”c”]

[/codesyntax]

In Line 1, the variable x is set to 0. The expression in the while statement (in Line 2), x <= 20, is evaluated. Because the expression x <= 20 evaluates to true, the body of the while loop executes next. The body of the while loop consists of the statements in Lines 3 and 4. The statement in Line 3 outputs the value of x, which is 0. The statement in Line 4 changes the value of x to 5. After executing the statements in Lines 3 and 4, the expression in the while loop (Line 2) is evaluated again. Because x is 5, the expression x <= 20 evaluates to true and the body of the while loop executes again. This process of evaluating the expression and executing the body of the while loop continues until the expression, x <= 20 (in Line 2), no longer evaluates to true.

whileExample

Kinds of while loops are: counter-controlled, flag-controlled, sentinel-controlled, EOF-controlled while loops and etc.

for Repetition (Loop) Structure

The general form of for loop is:

[codesyntax lang=”c”]

[/codesyntax]

The initial statement, loop condition, and update statement (called for loop control statements) enclosed within the parentheses control the body (statement) of the forstatement.
For Loop

The for loop executes as follows:

  1. The initial statement executes.
  2. The loop condition is evaluated. If the loop condition evaluates to true:
    1. Execute the for loop statement.
    2. Execute the update statement (the third expression in the parentheses).
  3. Repeat Step 2 until the loop condition evaluates to false.

The initial statement usually initializes a variable (called the for loop control, or forindexed, variable).

In C++,  for is a reserved word.

Concentrate on the example bellow:

[codesyntax lang=”c”]

[/codesyntax]

The initial statement, x = 0;, initializes the int variable x to 0. Next, the loop condition,x<10, is evaluated. Because 0<10 is true, the print statement executes and outputs 0. The update statement, x++, then executes, which sets the value of x to 1. Once again, the loop condition is evaluated, which is still true, and so on. When x becomes 10, theloop condition evaluates to false, the for loop terminates, and the statement following the for loop executes.

For Loop Example

do…while Repetition (Loop) Structure

This section describes the third type of looping or repetition structure, called a do…whileloop. The general form of a do…while statement is as follows:

[codesyntax lang=”c”]

[/codesyntax]

Do-While Loop

The statement executes first, and then the expression is evaluated. If the expressionevaluates to true, the statement executes again. As long as the expression in a do…while statement is true, the statement executes. To avoid an infinite loop, you must, once again, make sure that the loop body contains a statement that ultimately makes the expression false and assures that it exits properly.

[codesyntax lang=”c” container=”none”]

[/codesyntax]

The output of the code is:

0  5  10  15  20

After 20 output, the statement:

x = x +5;

changes the value of x to 25 and so x <=20 becomes false, which halts the loop.

  • In a while and for loop, the loop condition is evaluated before executing the body of the loop. Therefore, while and for loops are called pretest loops. On the other hand, the loop condition in a do…while loop is evaluated after executing the body of the loop. Therefore, do…while loops are called post-test loops. Because the while and for loops both have entry conditions, these loops may never activate. The do…while loop, on the other hand, has an exit condition and therefore always executes the statement at least once. Looping Control Structures

 

Previous How to become a computer programmer?
Next How to Project PC Screen to Mobile Screen

About author

Raza Bayani
Raza Bayani 42 posts

<p>Raza Bayani studies BCE (Bachelor in Computer Engineering) in Poly-technical University of Kabul. He is the CEO of CODEJOW group, a web designer, coder and recently a blogger. Raza thinks, he can help others and also boost-up his own programming skills being on codejow.com.</p>

View all posts by this author →

You might also like