Looping in Java

In this section we will discuss more about the Looping in Java.

Objective :

  • What is Looping in Java?
  • Types of Loop in Java
  • Implementation of Looping in Java

What is Looping in Java?

Looping is the process in which statement will be executed repeatedly for certain number of time. It may run for 1,2 3 …….. n times based on the condition provided. It has 2 part.

  1. Control Statement
  2. Body of the loop

In control statement, we need to provide the some condition based on which loop will continue and execute the body of the loop for certain number of time. When Condition is not satisfied then Control moved out from the loop and continues to run the corresponding statement.

Body of the loop is the statement whose execution is depending on the condition in Control statement. If Condition in control statement satisfied then body of the loop will be executed otherwise not. If condition is not satisfied then it will move out from the loop and continues to run the next statement out of the loop.

In Control statement, you can control the entry and exit criteria for the loop. Entry criteria is the control statement which control the starting of the loop. Whereas exit criteria control when to terminate the loop and go out of the loop and continue execution of the code.

Looping in Java

From the above diagram you can notice that looping process undergoes some steps. Such as

  • Checking the expression
  • If it is true the execute the statement of the body else it will exit the loop and go out of the loop
  • After executing the statement of loop body for true condition, It will increment of decrement the counter for checking the expression again.
  • Then it continues to the step 1. So on.

Types of Loop:

There are 3 type of loop available in Java.

  1. Entry Controlled loop
  2. Exit control Loop
  3. Jump statement

Entry controlled loop checks/ control the condition tested before the start of the loop.

For example:

  • while loop
  • for loop

Exit Controlled loop always check the condition after the end of the execution of the body unconditionally once. That means it will check  control the condition after loop running for once.

For Example :

  • do …. While  loop

Let’s understand each loop in detail

while loop:

This is  one of the Entry controlled loop. that means it checks/ control the loop before starting of the loop.

Syntax:

while ( Test condition)

{

Statement of the loop body

}

while loop will execute with some steps. Such as:

  • In first step it will check the condition which mentioned in while(). If it fails then it will go out of the loop.
  • If it passed the condition, then It will execute the statement of the loop body.
  • After execution of the statement of the body, It will come to the while condition again to check whether the condition still satisfying or not.
  • Like this it will continue the loop .
Looping in Java

Example:

 class checkwhile {
	public static void main(String args[])
	{
		int a = 4;
		int n = 0;
		while(n<a)
		{
			System.out.println("While loop is satisfying");
			n++;
		}
		System.out.println("Out of the wjile loop");	
	}
}

Output:

While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
Out of the While loop

Note: Do not forget to add the increment or decrement statement in the end of the statement of the loop. If you have not mentioned any increment or decrement control, then it will go into the infinite loop. see below example of infinite while loop.

class checkwhile {
	public static void main(String args[])
	{
		int a = 4;
		int n = 0;
		while(n<a)
		{
			System.out.println("While loop is satisfying");
			//n++;  Excluding the increment
		}
		System.out.println("Out of the While loop");
	}
}

Output:

While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
------------------------
------------------------
------------------------
While loop is satisfying
While loop is satisfying
------------------------
------------------------

do —- while Loop:

This loop always execute the statement of the loop body at least once even if it satisfy the while condition or not. I will check / control the condition after execution of the statement once. If it pass then continue for 2nd round of running of statement if loop body otherwise exit the loop and go out of the loop scope.

Syntax:

do{
Statement of loop body;
}while(Test condition);

do while loop will execute with some steps. Such as:

  • Execute the statement of the loop body.
  • Check the test condition mentioned in while()
  • If it failed in while () then redirect to exit of the loop and go out of the loop scope.
  • If it pass then it continues the step 1 which is “Execute the statement of the loop body”.
Looping in Java

Example:

class checkwhile {
	public static void main(String args[])
	{
		int a = 4;
		int n = 0;
		do
		{
			System.out.println("While loop is satisfying");
			n++;
		} while(n<a);
		System.out.println("Out of the wjile loop");	
	}
}

Output::

While loop is satisfying
While loop is satisfying
While loop is satisfying
While loop is satisfying
Out of the While loop

Let’s check if while loops fails for first repetition then what will happen.

class checkwhile {
	public static void main(String args[])
	{
		int a = 4;
		int n = 0;
		do
		{
			System.out.println("While loop is satisfying");
			n++;
		} while(n>a);
		System.out.println("Out of the wjile loop");	
	}
}

Output:

While loop is satisfying
Out of the While loop

From the above example you can notice that do..while loop always execute the body the loop at least once.

For Loop:

For Loops is one of the entry control loop type. So it will always check before beginning of the execution of the loop body.

Syntax:

for(Initialization ; Test condition ; increment or decrement)
{
Statement of the loop body;
}

From the above syntax you can notice that for loop ahs 3 argument.

  • Initialization
  • Test Condition
  • Increment or decrement

Initialization always occur when for loop is called. Post that it will check whether the test condition satisfies or no. If it satisfies then it will execute the statement of the loop body . Post this execution it will navigate to the 3rd argument which is increment or decrement and again check the test condition. Like this loop will continues until the test condition fails.

Once test condition got failed, it will go out fo the for loop and continue the execution statement mentioned after for loop.

Before going to the example of for loop, we need to understand some of the important features of the for loop.

Feature of for Loop:

  • You can initialize more than one variable at a time
  • Both Initialization and increment is the part of the for-loop statement
  • You can define complex calculation to check the test condition.
  • Same as initialization, multiple variable can also be a part of Increment argument in for loop.

Example:

class Checkforloop {
	public static void main(String args[])
	{
		int a = 4;
		
		for(int i=0; i<a; i++)
		{
			System.out.println("For loop is running for rep : " + i);
		}
		System.out.println("Out of For Loop");	
	}
}

Output:

For loop is running for rep : 0
For loop is running for rep : 1
For loop is running for rep : 2
For loop is running for rep : 3
Out of For Loop

break and continue:

In java, jumping from one statement to the end of the statement or beginning of the statement is possible. You can achieve this feature by using break and continue.

If you want to exit from the for loop before running all repetition then break will help you to do that. If you have used nested loop then break only moved you out of the current for loop.

In case of while and do….. while loop, continue control to go directly to the test the condition.

Syntax:

while (Condition )
{
Statement;
If(condition)
break
}
for(int  i=0; i<5; i++)
{
Statement;
if(Condition)
break;
Statement;  
}

Example:

class Checkbreakstatement {
	public static void main(String args[])
	{
		int a = 4;
		
		for(int i=0; i<a; i++)
		{
			System.out.println("For loop is running for rep : " + i);
			if(i==2)
				break;
			System.out.println("For loop is running for repitiion  : " + i );

		}
		System.out.println("Out of For Loop");	
	}
}

Output:

For loop is running for rep : 0
For loop is running for repitiion  : 0
For loop is running for rep : 1
For loop is running for repitiion  : 1
For loop is running for rep : 2
Out of For Loop

Here you can see if we use the break for i==2 condition then the loops will execute only till i==2 and then move out of the loop.

Example:

class Checkbreakstatement {
		public static void main(String args[])
	{
		int a = 4;
		
		for(int i=0; i<a; i++)
		{
			System.out.println("For loop is running for rep : " + i);
			if(i==1)
				continue;
			System.out.println("For loop is running for repitiion  : " + i );

		}
		System.out.println("Out of For Loop");
	}
}

Output:

For loop is running for rep : 0
For loop is running for repitiion  : 0
For loop is running for rep : 1
For loop is running for rep : 2
For loop is running for repitiion  : 2
For loop is running for rep : 3
For loop is running for repitiion  : 3
Out of For Loop

Leave a Reply

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