Cpp - do while Loops

Introduction

A loop can test the condition at the end of the loop with the do-while statement.

Consider the following loop:

int x = 60; 
do  
{ 
    std::cout << x << "\n"; 
    x++; 
} while (x < 50); 

This loop's conditional only is true when x < 50. Because x begins with an initial value of 60, this condition is never met.

A do-while loop always executes the body at least once.

Demo

#include <iostream> 
 
int main() /*from ww  w  . ja  va 2  s  . c  om*/
{ 
    int badger; 
    std::cout << "How many badgers? "; 
    std::cin >> badger; 
 
    do 
    { 
           std::cout << "Badger "; 
           badger--; 
    } while (badger > 0); 
     
    std::cout << "\n"; 
    return 0; 
}

Result