For loop vs while loop in java

As part of this article, we will try to understand what is for loop and a while loop also we will cover the difference between them.

For loop

Syntax

for (initialization; condition; iteration)
{
    //body of for loop
}

initialization – Here we will initialize the loop-controlling variable. The initialization of the variable will be done only once during the first iteration of the loop. We can create a loop-controlling variable either inside or outside of the loop. If the variable is created inside the loop then the scope of the variable will be inside the loop and we cannot access it outside the loop.

condition – If the variable matches the condition then the control goes inside the loop else not.

iteration – Here we define by how much value the variable can increment or decrement.

For loop explained

  • If we know the starting value and end value then we should use a for loop.
  • In the case of for loop, we will know the number of iterations.
  • For loop is an entry-controlled loop.
  • If the first condition is not true then the control will never enter a for loop.

For loop example

Say we have to print the numbers from 1 to 5. Here we know the below details

start-point = 1
end-point = 5
increment = By 1

package looping;

public class ForLoopExample {
	
	public static void main(String[] args) {
		
		int startPoint = 1;
		
		int endPoint = 5;
		
		for(int i=startPoint; i<=endPoint; i++)
		{
			System.out.println(i);
		}	
	}
}


Output :
1
2
3
4
5

While loop

Syntax

while (condition) 
{
  //body of while loop
}

condition – If the condition matches then the control goes inside the loop else not.

While loop explained

  • We can use a while loop when we don’t know the start point and end point.
  • In the case of the while loop, we will just know a supporting variable and we have to make use of that variable to control the loop.
  • We will not know the number of iterations in a while loop.
  • It is known as entry controlled loop.
  • If the first condition is not true then the control will never enter a while loop.

While loop example

Say we have the number 234567, we have to print all the even numbers within that number. Here we don’t have a start point or end point here we are just having a number.

1 ) So to do that we will first get the mod of the number. Mod of a number will print the remainder of the number.

remainder = number % 10;

remainder = 234567 % 10 = 7

2 ) Now we will check if the remainder is even. If the remainder is even then print it else not.

3 ) Now we will update the original number by removing the remainder.

number = number / 10 ;

number = 234567 / 10 = 23456

Repeat the above process in 3 steps till the number is 0. Hence we will keep the condition till the number is greater than 0.

package looping;

public class WhileLoopExample {
	
	public static void main(String[] args) {

		int number = 234567;
		
		while (number > 0)
		{
			int remainder = number % 10;
			
			if(remainder % 2 == 0)
			{
				System.out.println(remainder);
			}
			
			number = number/10;
		}
	}
}

Output :
6
4
2

Differences

For loopWhile loop
1 ) We should know the start value and end value to use this loop.1 ) We should not know the start value and end value to use this loop. We just need a supporting variable.
2 ) We will know the number of iterations in for loop.2 ) We will not know the number of iterations in the while loop.
3 ) for loop consist of 3 parts they are initialization, condition, and iteration.3 ) while the loop consists of only 1 part (i. e) condition.
4 ) Iteration statement is written at the top of the for loop.4 ) Iteration statement can be written anywhere in the while loop.
5 ) If the condition is not defined then the loop iterates an infinite number of times.5 ) If the condition is not defined then a compilation error is thrown.

In this article, we have covered what is for loop and a while loop also we have covered the difference between them. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send an email.

Leave a Comment