LOOPS in C++

C++ Loops

There may be a situation when we need to execute a block of the program several times. Therefore, In a programming language, loops are used when we need to execute a block of statements repeatedly.

The following is the general from of a loop statement:

There are mainly two types of Loops:

  1. Entry Controlled loops: The test condition is tested before entering the loop body. While Loop and For Loop is entry-controlled loops.
  2. Exit Controlled Loops: The test condition is tested at the end of the loop body. Hence, the loop body will execute at least once, irrespective of whether the test condition is true or false. The do-while loop is exit controlled loop.

Concept of Loop statements

Sr.NoLoop Type & Description
1while loop– Repeats a statement or group of statements while a given condition is true. It evaluates the condition before executing the loop body.
2for loop– Execute a sequence of statements multiple times and reduces the code that manages the loop variable.
3do…while– Just like a while statement, only difference is that it tests the condition at the end of the loop body.
4nested loops– A loop inside another loop is called a nested loop. 

C++ While Loop

Syntax:

while (condition) 
{
    // body of the loop
}
  • while loop assess the condition
  • If the condition assessment is true, the code inside the while loop is executed.
  • Then the condition is assessed again.
  • This process continues until the condition assessment is false.
  • When the condition assessment is false, the loop terminates.

C++ for LOOP

for (initialization; condition; update) 
{
    // body of-loop 
}

Here,

  • initialization – Variable is initialized and is executed only once
  • condition – If the assessment of the condition is true, the body of for loop is executed and
    if false, the for loop gets terminated
  • update – Updates the value of initialized variables and checks the condition again

C++ do…while loop

Here,

  • At first, the body of the loop is executed. Then the condition is evaluated.
  • If the evaluation of the condition is true, the body of the loop inside the do statement is executed again.
  • The condition is evaluated again.
  • If the condition assessment is true, the body of the loop inside the do statement is executed again.
  • This process keeps repeating until the condition evaluates to false. Then the loop terminates.

Loop Control Statements

Loop control statements change the execution of the program from their normal sequence.

Following are the control statements in C++:

Sr.NoControl Statement & Description
1break statement– Terminates the loop and transfers execution to the statement immediately following the loop. Also used in switch statements.
2continue statement– Causes the loop to skip the remaining body and immediately retest its condition prior to reiterating.
3goto statement– Transfers control to the labeled statement.

The Infinite Loop

A loop becomes an infinite loop if the evaluation of the condition is never false.

Example:

#include <iostream>
using namespace std;
int main(){
   for(int i=1; i>=1; i++){
      cout<<"GeeksGod"<<endl;
   }
   return 0;
}

OUTPUT
------
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
GeeksGod
...

Also,

#include <stdio.h>

int main()
{
    for(;;)  // Absent Condition
    {
    printf("GeeksGod\n");
    }
    return 0;
}

When the conditional expression is absent, it is assumed to be true.

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?