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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s