The while and do

the syntax and programming examples on the use of do and do while loops.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

while loop is used to repeatedly execute a set of statements. This is called looping. A while loop continues to execute so long as its condition expression evaluates to true. This is the syntax of a while loop.

while(condition-expression)
{
    statement1;
}
Before executing statement1, the condition-expression is evaluated. If it evauates to a boolean true value, then the statement1 is executed, otherwise the loop terminates.

break statement

A break statement inside the body of a loop(this applies to other loops as well) causes a loop to terminate. In the program below, the break statement terminates the loop. So the numbers are printed till 5 only.
int main()
{
    int x = 0;
 
    while (x < 10)
    {
        if(x > 5)
        {
            break;
        }
 
        cout << x << endl;
 
        x++;
    }
    return 0;
}

continue Statement

The continue statement causes the remaining statements to be bypassed, and the control is again transferred to the top of the loop condition. In the following example, the continue statement prevents the printing of numbers that are smaller than 5.

int main()
{
    int x = 0;
 
    while (x < 10)
    {
        x++;
 
        if(x < 5)
        {
            continue;
        }
 
        cout << x << endl;
    }
 
    return 0;
}

while loop is like a pre-paid service

A while loop can be compared to a pre-paid mobile connection. Condition is tested prior to the execution of it's statements.

do-while loop

A do-while loop executes atleast once. The condition is tested after executing the statements. It's similar to a post-paid service. It's syntax is as follows -

do
{

    statement1 ;

}while(condition-expression);

This loop first executes statement1, and then tests the condition-expression. After that it continues to repeat the statements till the condition remains true, or a break statement is encountered.

while loop

Write a program that asks the user to enter a number. Then display the square of that number, and ask the user if he wants to enter another number. Keep obtaining numbers and displaying the squares, so long as the user keeps answering in the affirmative.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{
    cout << "Proceed?[y/n]: "; 
            
    char ansr; 
         
    cin >> ansr;
         
    while ('y' == ansr || 'Y' == ansr)
    {
        cout << "Enter a number: " << endl;
             
        int num; 
                
        cin >> num; 
                  
        cout <<"Square is = " << num * num << endl; 
                 
        cout << "Continue?[y/n]: ";
             
        cin >> ansr;
    }
 
    cout << "Quitting..." << endl;
 
    return 0;
}
 

 

while loop - Odd number series

Write a program to find odd number less than 100 using while loop.
#include<iostream> 
#include<cstdio> 
 
using namespace std ;
 
 
int main() 
{
      
    int i = 1; 
      
    while( i < 100 )
    {
          
        int a = i % 2;
              
        if( 0 != a )
        { 
               
            cout << i << endl;
                   
        }
            
        i++;
            
    }
     
    return 0;
      
}

Alternate Solution

Following is an alternate solution that uses the shortcuts of C very well.
int main()
{
    int i = 0; 
 
    while( ++i < 100 )
    {
        if( i % 2 )
        { 
            cout << i << endl;
        }
    }
 
    return 0;
}

while loop - Series

Program to print the following series by using 'while loop' 125 , 64 ,27 , 8 ,1. This is a series of cubes of natural numbers - 53, 43, 33, 23, 13.
#include<iostream> 
#include<cstdio> 
 
using namespace std ;
 
 
int main() 
{
 
      
    int i = 5;
    
    while( i >= 1)
    {
        
        printf( "%d\n" , i * i * i );
    
        i = i - 1;
    
    }
 
    return 0;
      
}

do-while

Write a program that asks the user to enter a number. Then display the square of that number, and ask the user if he wants to enter another number. Keep obtaining numbers and displaying the squares, so long as the user keeps answering in the affirmative. Use a do-while loop.

#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
{        
    char ansr; 
         
    do
    {
        cout << "Enter a number: " << endl;
             
        int num; 
                
        cin >> num; 
                  
        cout <<"Square is = " << num * num << endl; 
                 
        cout << "Continue?[y/n]: ";
             
        cin >> ansr;

    }while ('y' == ansr || 'Y' == ansr);
 
    cout << "Quitting..." << endl;
 
    return 0;
}
 

 

while loop - Reverse a number

Write a program to find the reverse of a number.
#include<iostream> 
#include<cstdio> 
 
using namespace std ;
 
int main() 
{      
    int r, n, b;
 
    b = 0;
    
    cout << "Enter number";
    
     cin >> n;
 
    while (n > 0)
    {
    
        r = n % 10;
    
        n = n / 10;
    
        b = ( b * 10 ) + r;
 
    }
 
    printf("%d",b);
 
    return 0;
      
}

A Shorter and Alternate Solution

Here is a shorter and alternate solution that uses the various shortcuts of C.

int main()
{
    cout << "Enter a positive number:";
    
    int n;
 
     cin >> n;
 
    int result = 0;
 
    do
    {
 
        (result *= 10) += (n % 10);
 
    }while (n /= 10);
 
    cout << result << endl;
 
    return 0;
}

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video while and do while loops



Creative Commons License
This Blog Post/Article "The while and do" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05