Loops in a programming language is a piece of code that get executed repeatedly until the specified condition becomes false. A loop consists of two parts, a block of statements and a condition that control the loop.

Loops mainly consisted of three statements – initialization condition, test condition, update condition.

For example, if we want to print “Hello Codies” 5 times, we can do this in two ways. Writing the statement 5 times (#1) or use loops which save time and effort (#2).

Java

1

#1

2

3

#include <stdio.h> 

4

int main() 

5

{ 

6

     printf("Hello Codies\n"); 

7

     printf("Hello Codies\n");

8

     printf("Hello Codies\n");

9

     printf("Hello Codies\n");

10

     printf("Hello Codies\n");

11

12

    return 0; 

13

}

Java

1

#2

2

3

#include <stdio.h> 

4

5

int main() 

6

{ 

7

  int i; 

8

9

    // Writing a for loop 

10

    // to print Hello Codies 5 times 

11

12

    for (i = 1; i <= 5; i++) { 

13

        printf("Hello Codies\n"); 

14

    } 

15

    return 0; 

16

}

Output:

Java

1

Hello Codies

2

Hello Codies

3

Hello Codies

4

Hello Codies

5

Hello Codies

In this tutorial, you will learn-

  1. Types of Loops
  2. While Loop
  3. Do-While loop
  4. For loop
  5. Nested loop
  6. Break Statement
  7. Continue Statement

Types of Loops in C/C++

Loops are classified into two types depending upon the position control statements:

  • Entry controlled loop
  • Exit controlled loop

Entry controlled loop: A conditional statement is evaluated before executing the body of a loop. It is also called as a pre-checking or Counter controlled loops where the programmer already know how many times the loop get executed. Counter controlled loops are often called definite iteration because the number of repetitions is known before the loop begins execution.

Exit controlled loop: In this type of loops conditional statement are evaluated after executing the loop body once irrespective of whether the test condition is true or false. It is also called as a post-checking or Sentinel controlled Loop because the programmer don’t know exactly know how many times loop will be executed. Sentinel-controlled loop also known as indefinite repetition.

The control conditions must be specified properly otherwise the loop will execute infinite number of times (infinite loop). An infinite loop occurs when no termination condition is specified or specified conditions never meet.

programming language provides us with 3 types of loop and C++ has 4th extra loop:

  • while loop
  • do-while loop
  • for loop
  • Nested Loop (only in C++)

The syntax of these loops mainly differs in the placement of these three statements initialization condition, test condition, update condition.

While Loop in C/C++

The while loop specify that a program should repeat set of instruction inside the block till the condition remains true. While loop used in place where we don’t know number of iteration before and it depends on the update inside the block. While loop is Entry Controlled loop and widely used in C/C++.

Syntax of WHILE Loop:

Java

1

initialization condition;

2

while (test_ condition)

3

{

4

   // statements

5

6

  update_ condition;

7

}
Flow Chart of While loop.

In While loop, condition evaluated first if condition is TRUE than block of statements get executed and update condition then control again goes back at the beginning, and the condition is checked again if it is true, the same process is executed until the given condition becomes false. Once the test condition results FALSE control goes back to main program. While Loop is Entry-Controlled Loop.

Note: If Loop contains only one statement, then the curly braces are not compulsory. Though It is a good practice to include braces.

Following program illustrates a while loop:

Java

1

#include<stdio.h>

2

3

int main()

4

{

5

  int num=1;    //initializing the variable

6

7

     while(num<=5)            //while loop with test condition

8

    {

9

       printf("%d ",num);  

10

       num++;        //incrementing (update condition)

11

    }

12

    return 0;

13

}

Output:

Java

1

1 2 3 4 5

The above program prints series of numbers from 1 to 5 using a while loop.

Let’s see what happening in above program step by step.

Step 1: We have initialized variable ‘num = 1’.

Step 2: In while condition we are evaluating is ‘num’ value is less than or equal to 5, if ‘num’ becomes greater than 5 than this condition become False.

Step 3: For now ‘num = 1’, we enter the loop comparing 1 <= 5 which result true so the body of loop i.e. ‘num’ value 1 got printed.

Step 4: In this step ‘num’ value increased by 1 i.e now ‘num = 2’.

Now step 3 get repeated again with ‘num = 2’ and print the ‘num’ value 2, similarly 3,4,5. When ‘num’ got increase to 6 (num =6) while condition become FALSE & loop terminated than control goes back to main() function as there is nothing after loop do program exits with returning 0 indicating that everything went well.

#tutorial #iot #c++ #c #c++ beginner course #learn c++

Types of Loops in C and C++? - DZone IoT
1.15 GEEK