Home Java Beginners do-while Loop in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

do-while Loop in Java

Advertisement
In this section you will learn how to use the do-while statement in java.

do-while Loop in Java

     

In some conditions in java, you need to run some statements and processes once at least. For that you have to use the do while loop in  after that check the condition then we use the do-while loop statement. do-while loop is same as the while loop statement but while loop checks the certain condition is first and if condition is true then statements or processes are executed otherwise all the statements written under the while loop is ignored by the interpreter but do - while loop executes all the statements first at once and then check the condition if the condition is true then all the statements are also executed in second times otherwise second times ignored all the statements.

The Syntax for the do-while loop:

  do
  {
  statements;
  }
 
while(condition);

In this example you will see how to use the do-while loop statement. The values are already given the variable n and r in the program after that it calculate the program and its perform the its original number and its reverse number.

Here is the code of the program:-

public class DoWhile{
 
 public static void main(String[] args){
  int n = 12345;
  int t,r = 0;
  System.out.println("The original number : " + n);
 
 do{
  = n % 10;
  r = r * 10 + t;
  n = n / 10;
  }while (n > 0);
  System.out.println("The reverse number : " + r);
  }
}

Download The do - while Example

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Ask Questions?    Discuss: do-while Loop in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
jacinth
August 31, 2011
confused?

what if in condition on while im using string above what should be the format? like these package Passes; import java.util.Scanner; public class Sc { public static void main(String[]args){ Scanner jaz = new Scanner(System.in); do{ System.out.println("press c"); String c = jaz.next(); System.out.println("input age: "); int age = jaz.nextInt(); if(age < 60){ System.out.println("young!"); }else System.out.println("guwang!"); }while (String.e) }
winnie
September 21, 2011
java

i need to get help about java programing
gcebile nhleko
November 17, 2011
thrilled

i like your examples.
murugan
January 25, 2012
java programming

simple program exercise very helpful java at j2ee kindly help me..
shamim
February 6, 2012
please help me , To unnderstantd this program

I can not understand this program public class DoWhile{ public static void main(String[] args){ int n = 12345; int t,r = 0; System.out.println("The original number : " + n); do{ t = n % 10; r = r * 10 + t; n = n / 10; }while (n > 0); System.out.println("The reverse number : " + r); }
Noor Suhadah
February 7, 2012
loop with while_for_do while

1. for 2. while 3. do while display the following for loop 1 12 123 1234
daphnie
March 6, 2012
blocking username and password..

i am requesting for a code for that matter
enrique demon
April 23, 2012
sr

its a nice program
Balaji Sankar
May 28, 2012
doubt

Please provide a step by step process of how the program s working
rjhoi
July 30, 2012
OOP

can you please add more examples?
aslima asim ali
September 13, 2012
java

actualy it is not a comment:i want to ask how to make a do while loop in java program?can you help me pls :)
Abhishek gautam
June 25, 2013
core java

nested do while loop or do while A AB ABH ABHI ABHIS what is the source code
j.sumithvictor
July 26, 2013
java

its very nice to student
Lewis
October 4, 2013
Java

Question print multiples of 37 below 100 using do while loop
DMCA.com