Java Programming | Using Do-While Loops

Introduction

This is a tutorial for using do-while loops in java. The program is given below that prints 1 to 10 using do-while loop in java. The program is extendable. Go enjoy the program. Lets begin…….

Program for printing 1 to 10 using do-while loop in java.


// the name of our class its public
public class DoWhileLoop {
//void main
public static void main (String[] args)
{
//declare int
int i=1;
//while loops
do
{
System.out.println(i);
i++;
}while(i<=10);
}
}

Output

1
2
3
4
5
6
7
8
9
10

How does it work

The numbers are directly printed. no need to input.

Extending it

The program can be extended by replacing the 10 by the limit in the program. You can also let user enter the limit that can be saved in a variable and replaces 10 in the program. Go ahead and extend it…..

Explanation.

  1. Declare the class as public
  2. Add the void main function
  3. Add the do-while loop
  4. Add system.out.println() function to print the number.
  5. Add i++.

At the end.

You learnt creating the Java program for Using Do-While loops. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Join India's Largest Code Learning Community on Uphub.io Get it on Google Play
Sharing options: