Skip to toolbar

Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)

           While and do-while are two more alternates of for loop in Java. The only representation of these statements is different.

while Loop

while Loop
while Loop

          While loop consists only one parameter, based on that it repeats the instructions given in while block.

       A while loop first checks the condition and then executes the instruction given in the block.

Code Example-1:

public class WhileExample {

	public static void main(String[] args) 
	{
		int counter=1;
		// First checks the condition then executes the code block.
		while(counter<=5)
		{
			System.out.println(counter);
			counter++; 
		}
	}
}

Output:
1
2
3
4
5

do-while Loop

do-while Loop
do-while Loop

           The key difference between while and do-while loop is that while loop first checks the condition then executes the code block. On the other hand do-while loop first executes the code block then checks the condition.

In this case, code of block would execute at least once.

Code Example:

public class DoWhileExample2 {

	public static void main(String[] args) 
	{
		int counter=1;
		do
		{
			// First executes the code block then checks the condition.
			System.out.println(counter);
			counter++; 
		}while(counter<=5);
	}
}

Output:
1
2
3
4
5

Difference between while and do-while loop:

public class WhileExample {

	public static void main(String[] args) 
	{
		int counter=1;
		while(counter<=0)
		{
			System.out.println("Value of counter: "+counter);
			counter++; 
		}
		System.out.println("End of Program.");
	}
}

Output:
End of Program.
public class DoWhileExample {

	public static void main(String[] args) 
	{
		int counter=1;
		do
		{
			System.out.println("Value of counter: "+counter);
			counter++; 
		}while(counter<=0);
		
		System.out.println("End of Program.");
	}
}

Output:
Value of counter: 1
End of Program.

2681total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 3
  • Total visitors : 48,110
  • Total page view: 66,245

A pointer is a variable which contains the address of another variable. Or we can say that a pointer is a variable whose value is the address of another value.

Facts about main() Method
  1. A main() method is an entry point for a java program.
  2. main() would be called by the JVM using the class directly without creating an object. That’s why we declare the main method ‘public’ and static.
  3. main() would never return any value, so we kept return type void.
  4. main() would take parameters in the form of String Array. Which would be used to passing input values at runtime.
To get more information please visit:
Basic Java – 3 || Understanding Class, Objects, Methods in Java.

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.