chevron_left
399 points
8 4 3

Difference between while and do while in java

while loop:

While loop statement:

While loop statement is one of the easiest and most widely used looping constructs in the programming language.

In this statement, while it is a reserved word or keyword, the condition can be any constant, variable, expression, and statement can be a single statement or a group of statements. In the while loop, we have to perform three steps:

  • Initialization
  • Test Condition
  • Increment and decrement

Initialization Value: In a while loop, the first step is initialization is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter in while loop.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the while loop is to check the test condition whether the loop is executed or not. If the condition is true in the while loop, the loop's body is executed; otherwise, there is no loop execution.

For Example: while (i<=10)

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
  • C++
  • Java

 



#include <iostream>

using namespace std;

  

int main()

{

  

    int i = 5;

  

    while (i < 10) {

        i++;

        cout << "GFG\n";

    }

  

    return 0;

}

 

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.

do-while loop statement:

In the C programming language, the do-while loop statement is also similar to a while loop in the C programming language. In this, first of all, the loop's body is executed then the condition is checked. In the do-while loop, we have to perform three steps:

ADVERTISEMENT

 

Advertisement report

  • Initialization
  • Test Condition
  • Increment and decrement

Initialization value: In a do-while loop, the first step is initialization and is used to set the initial value of the loop counter. The loop counter may be an increment counter or decrement counter.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the do-while loop is to check the test condition whether the loop is executed or not.

For Example: while (i<=10);

Increment and decrement: After checking the test condition in the do-while loop, the next step is to increment and decrement the loop counter's value.

For Example: i = i + 1;

Syntax:

do
{
    statements..
}
while (condition);

Flowchart:
do-while

Example:

  • C
  • C++
  • Java

 

 

 

#include <iostream>

using namespace std;

  

int main()

{

  

    int i = 5;

  

    do {

        i++;

        cout << "GFG\n";

    } while (i < 10);

  

    return 0;

}

 

Output:

GFG
GFG
GFG
GFG
GFG

Here is the difference table:

whiledo-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);

1 Comment

I have learned new things today. Keep posting :)

More Posts

Difference between for and while loop Sanjana Sagar - May 29
test if a number input from the keyboard is a valid test score (between 0 and 100 inclusive).Using for,while,do-while DivyaN - Aug 19
Difference between static and dynamic in java Sanjana Sagar - May 29
Difference between abstract and interface in java Sanjana Sagar - May 28
Difference between abstract and interface in java Sanjana Sagar - May 28
Difference between class and method in java Sanjana Sagar - May 20
Difference between nested class and inner class in java Sanjana Sagar - May 19
Difference between static and nonstatic methods in java Sanjana Sagar - May 19
Difference between error and exception in java Hasnain_khan - Oct 13, 2020
What is the difference between java and core java Sanjana Sagar - May 28