Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

View RSS Feed

JD1's Personal Development Blog

Do While Loop

Rate this Entry
We use the Do While Loop when we want to run a set of instructions before testing to see if a condition is true. The difference between a Do While Loop and a While Loop is that the While Loop tests the condition first and then runs the instructions.

class Class1{
	public static void main(String args[]){
		int counter = 0;
		do{
			System.out.println(counter);
			counter++;
		}while(counter <= 10);
	}
}

We have a counter initialised to zero. It's going to then output the counter as zero and then increment it by 1. It then checks to see if the while section will permit it to iterate again.

If our counter had been initialised to 50, it would output 50 and then end, because the while section sees that the counter isn't actually <= 10.

The only perameter used in the while part is when you want it to iterate. In this case it's while the counter is less than or equal to 10. Simple enough?
Categories
Uncategorized

Comments