Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Do-while loop in Java programming language

Posted on December 6, 2016September 28, 2020

Table of Contents

  • Do-while loop in Java programming language
    • Loops in Java
    • Do while loop in Java
    • Example programs
    • Nested do while loop in Java
    • Related

Do-while loop in Java programming language

Loops in Java

In this article, we will discuss the Do-while loop in the Java programming language

Loops are an important concept in java language. It is used to multiple circulations(more than one)

in the Java, three types of basic loops available in Java

 

  • for loop
  • while loop
  • Do-while loop
 

Do while loop in Java

the do-while loop is like to a while loop  but it is guaranteed to execute at least one time before the test expression is checked

It contains two-part

  • Do part – the start of the loop
  • while part – end of the loop

At the do-while loop, Boolean expression includes at while part – end of the loop, so  the statements in the loop execute once before the boolean is checked

When control of loop checks boolean expression in the while part, if the boolean expression is true, the loop goes to do part and executed again. , this process continues until the boolean is false

Syntex

this is the syntax of the loop

do{
// statement(s);
// increments

} while (check Boolean_expression);





flow diagram of the loop

 

Example programs


class DoWhileDemo{
public static void main(String args[]){
int x=1;
do{
System.out.println(“x is – “+x);
x++;

}while (x<11); //condition is true

}

}

When the above code is executed, it produces the following result



The following program is the same but another situation – the Test expression is false

class DoWhileDemo{
public static void main(String args[]){
int x=1;
do{
System.out.println(“x is – “+x);
x++;

}while (x>11); //condition is false

}
}

When the above code is executed, it produces the following result

do-while loop is guaranteed to execute at least one time before checking the test expression

 

Program 3

Calculate the sum of natural numbers using do-while loop

import java.util.Scanner;
class Sum_Of_Num1{
public static void main(String args[]){
  int sum=0;
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the number as you wish: ");
int num=scan.nextInt();//get input from the user for num
int i=1;
do{
  sum+=i;//sum=sum+i;
  i++;
}while(i<=num);
System.out.println("the sum of 1 to "+num+"is: "+sum);

}


}

When the above code is executed, it produces the following result

Enter the number as you wish:100
the sum of 1 to 100 is:5050

 

 

Nested do while loop in Java

Example
 
class NestedDowhileloop

 

class dowhile{
public static void main(String args[]){
int row=1,coloum=1;
int x;
do
{
x=4;
do{
System.out.print(“”);
x–;
}while(x>=row);
coloum=1;
do{
System.out.print(row+” “);
coloum++;
} while(coloum<=row);
System.out.println(” “);
row++;
}while(row<=5);
}
}

 

When the above code is executed, it produces the following result

 

Suggested post
While Loop in Java       while Loop in C++     While Loop In C

For Loop in C++           For Loop in C         for loop in java

If condition C++        If condition in Java       If condition in C

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes