Related Articles

Related Articles

Difference between while and do-while loop in C, C++, Java
  • Last Updated : 06 Jun, 2019

while loop:

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.
Syntax :

while (boolean condition)
{
   loop statements...
}

Flowchart:
while loop

Example:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
  
    int i = 5;
  
    while (i < 10) {
        printf("GFG\n");
        i++;
    }
  
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
  
    int i = 5;
  
    while (i < 10) {
        i++;
        cout << "GFG\n";
    }
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        int i = 5;
  
        while (i < 10) {
            i++;
            System.out.println("GfG");
        }
    }
}

chevron_right


Output:



GFG
GFG
GFG
GFG
GFG

do-while loop:

do while loop is similar to while loop with the only difference that it checks for the condition after executing the statements, and therefore is an example of Exit Control Loop.

Syntax:

do
{
    statements..
}
while (condition);

Flowchart:
do-while

Example:

C

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
  
int main()
{
  
    int i = 5;
  
    do {
        printf("GFG\n");
        i++;
    } while (i < 10);
  
    return 0;
}

chevron_right


C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
  
    int i = 5;
  
    do {
        i++;
        cout << "GFG\n";
    } while (i < 10);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        int i = 5;
  
        do {
            i++;
            System.out.println("GfG");
        } while (i < 10);
    }
}

chevron_right


Output:

GFG
GFG
GFG
GFG
GFG

Here is the difference table:

while do-while
Condition is checked first then statement(s) is executed. Statement(s) is executed atleast once, thereafter condition is checked.
It might occur statement(s) is executed zero times, If condition is false. At least once the statement(s) is executed.
No semicolon at the end of while.
while(condition)
Semicolon at the end of while.
while(condition);
If there is a single statement, brackets are not required. Brackets are always required.
Variable in condition is initialized before the execution of loop. variable may be initialized before or within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition)
{ statement(s); }
do { statement(s); }
while(condition);

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :