Bitsmore360 - Top trends, Tech News, Tips and Tricks: Java Ques 101: While Loop in Java with examples

Java Ques 101: While Loop in Java with examples

In this article, "Java Ques 101: While Loop in Java with examples" we will learn the basics of while loop in Java. We will also see some of the examples of while loops. The loop executes statements repetitively up to a specified number of times. Earlier we've read about the Java for loop. Later we will also differentiate between for and while loops.



Java Ques 101: While Loop in Java with examples



Java While Loop


The Java While loop is a control statement that allows you to repeat a block of code for a specific number of times until the condition is true. It is also known as an iterative statement. It is used when you don't know in advance the number of times the loop should be run.

Syntax:

while(Condition)
{
//Statement 
increment/decrement expression
}


Flow Chart: A flow chart is a diagrammatic representation of an algorithm. It represents the workflow or process in a sequential manner. Given Below is a flow chart of "While loop".





Example: Write a simple program in java to print Hello using While loop.


1
2
3
4
5
6
7
8
9
public class Demo {  
public static void main(String[] args) {  
int i=1;  
while(i<=5){  
System.out.println("Hello");  
i++;  
}  
}  
}  

Output:-




In First Iteration, the value of i will be 1, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line. 

In Second Iteration, the value of i will be 2, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.

In Third Iteration, the value of i will be 3, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line.

In Fourth Iteration, the value of i will be 4, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line. 

In Fifth Iteration, the value of i will be 5, it is less than 5 so condition becomes true and will execute inner statement. The command System.out.println("Hello") will print hello and change the line. As soon as the condition become falls i.e i becomes greater than 6 loop will stop executing the command.


Difference between For Loop and While Loop:



For Loop While Loop
For Loop is used when number of iteration is known in advance. While Loop is used when number of iteration is not known.
For Loop is entry controlled loop. While Loop is entry controlled loop.
For Loop Syntax:

for(Initialization; Test Expression; Update Expression) { //Statement }
While Loop Syntax:

while(Condition) { //Statement increment/decrement expression }
In For Loop initialization of variable can be done inside or outside of Loop. In While Loop initialization of variable is done outside of Loop



Did you find this article helpful? OR Have any doubt, write in the comment section. To learn more about Bitsmore360, check out the rest of our Blog, and visit our Pinterest site and follow us on Instagram.


No comments:

Post a Comment