Do-while Loop in C++

Do while loop

Here, in this section, we will discuss the do-while loop in C++. Do while is a looping structure in C++, these types of loops are called Exit control or Post-test loops. 

do while

Looping in C++ 

We have the following loops in C++ :

  • While loop
  • For loop
  • Do while
  • Nested loops

We will discuss do-while loop in this post –

Do while

Very similar to the while loop. To do repetitive work control structure has been created.

In do while we execute some statements first and then check a condition. If the condition is met we execute it again. We keep doing this until the condition keeps meeting.

Syntax

do{
    // execute statement(s)
}while(condition(s))

Along with increment and initialization logic it may look like 0

initialization;

do{
    // execute statement(s)
    incrementation;
    
}while(condition(s))
Do while Loop in C++

Let us learn do-while looping with a few examples –

Example 1

#include<iostream>
using namespace std;
 
int main()
{
    int i = 1; // initialization
    
    do{
        cout << i << ". Hello World\n";
        i++; // incrementation
    }while(i <= 5); 
    // condition(s) checking
    
    return 0;
}

Output

1. Hello World
2. Hello World
3. Hello World
4. Hello World
5. Hello World

The following examples show how it will be implemented at least once –

Example 1.1

#include<iostream>
using namespace std;
 
int main()
{
    int i = 1000;
    
    // i is set as 1000 which is definitely greater than 5
    // the condition set below is (i <= 5) which will surely be never met
    // as the loop is incrementing (i++)
    // but still the loop is executed once anyways
    
    do{
        cout << i << ". Hello World\n";
        i++; 
    }while(i <= 5); 

    return 0;
}

Output

1000. Hello World

Example 2

Program to print first n numbers.

#include<iostream>
using namespace std;
 
int main()
{
    int n = 25;
    
    int i = 1;
    do{
        cout << i << " ";
        i++; 
    }while(i <= n); 

    return 0;
}

Output

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 

Example 3

Program to print numbers divisible by 7 up to 100.

#include<iostream>
using namespace std;
 
int main()
{
    int n = 100;
    
    int i = 1;
    do{
        // if divisible by 7 must leave 0 remainder
        // on applying modulo operator
        if(i % 7 == 0){
            cout << i << " ";
        }
        i++; 
    }while(i <= n); 

    return 0;
}

Output

7 14 21 28 35 42 49 56 63 70 77 84 91 98 

Example 4

Program to print items in a character array

#include<iostream>
using namespace std;
 
int main()
{
    char a[] = {'P', 'o', 'k', 'e', 'm', 'o', 'n'};
    
    int i = 0; // array index starts from 0
    do{
        cout << a[i] << " ";
        i++; 
    }while(i < 7); 

    return 0;
}

Output

P o k e m o n 

Example 5

Program to print Ascii Values

#include
using namespace std;
 
int main()
{
    char i='A';//inialisation
    do
    {
        cout << i << " : " << (int) i << " ";
        i++; // ascii codes are incremented
        
    } while(i<='Z'); // internally ascicodes are compared 
}

Output

A : 65 B : 66 C : 67 D : 68 E : 69 
F : 70 G : 71 H : 72 I : 73 J : 74 
K : 75 L : 76 M : 77 N : 78 O : 79 
P : 80 Q : 81 R : 82 S : 83 T : 84 
U : 85 V : 86 W : 87 X : 88 Y : 89 Z : 90