Loops in Java

Advertisements

Java Loops

Loops are used, when we need to execute one or more statements multiple times until some conditions are satisfied. Syntax of loops are similar with C / C++ language.

Types of Loops

  • While loop
  • Do-While loop
  • For loop
  • Enhanced For loop (not available in C / C++ )
  • Labelled loop (not available in C / C++ )

Java While Loop

While loop is also called entry control loop because, in while loop, compiler will 1st check the condition, whether it is true or false, if condition is true then execute the statements.

Syntax of while loop


	initialization;

	while(condition)
	{
		- - - - - - - - - -
		- - - - - - - - - -

		 inc/dec;
	}

Example of while loop


	//WhileLoopDemo.java

	import java.util.Scanner;

	class WhileLoopDemo
	{
		public static void main(String args[])
		{
			int a=1,num;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter any number : ");
			num = Integer.parseInt(sc.nextLine());

			while(a<=num)
			{
				System.out.print("\n\t\nHello...!!");
				a++;
			}
		}
	}

   Output :

			  Enter any number : 5
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!

Java Do-While Loop

do...while loop is also called exit control loop because, in do...while loop, compiler will 1st execute the statements, then check the condition, whether it is true or false.

Syntax of do-while loop


	initialization;

	do
	{
		- - - - - - - - - -
		- - - - - - - - - -

		 inc/dec;
	}while(condition);

Example of do-while loop


	//DoWhileLoop.java

	import java.util.Scanner;

	class DoWhileLoop
	{
		public static void main(String args[])
		{
			int a=1,num;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter any number : ");
			num = Integer.parseInt(sc.nextLine());

			while(a<=num)
			{
				System.out.print("\n\t\nHello...!!");
				a++;
			}
		}
	}

	Output :

			  Enter any number : 5
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!

Difference between while and do-while loop

while loopdo...while loop
It is entry control loop.It is exit control loop.
In this loop condition is checked before loop execution.In this loop condition is checked at the end of loop.
It will never execute loop if condition is false.It will executes loop at least once when the initial condition is false.
There is no semicolon at the end of while statementThere is semicolon at the end of while statement.

Java For Loop

In for loop we put initialization, contidion and increment/decrement all together. Initialization will be done once at the beginning of loop. Then, the condition is checked by the compiler. If the condition is false, for loop is terminated. But, if condition is true then, the statements are executed until condition is false.

Syntax of for loop


	for(initialization;condition;inc/dec)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

Example of for loop


	//ForLoopDemo.java

	import java.util.Scanner;

	class ForLoopDemo
	{
		public static void main(String args[])
		{
			int a=1,num;
			Scanner sc = new Scanner(System.in);

			System.out.print("\n\tEnter any number : ");
			num = Integer.parseInt(sc.nextLine());

			while(a<=num)
			{
				System.out.print("\n\t\nHello...!!");
				a++;
			}
		}
	}

	Output :

			  Enter any number : 5
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!
			  Hello...!!

Java Enhanced Loop

Enhanced for loop is especially designed to travel each element of an array, without explicitly specify the size of array.

Syntax of enhance loop


	for(data-type var-name : array-name)
	{
		- - - - - - - - - -
		- - - - - - - - - -
	}

Example of enhance loop


	//EnhancedForLoop.java

	import java.util.Scanner;

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

	     int[] arr = new int[]{78, 45, 12, 89, 56};

	     System.out.print("\nDisplay values, size taken from built-in length() function.\t");
	     for(int i=0;i < arr.length;i++)
		     System.out.print(arr[i] + ", ");

	     System.out.print("\n\n\tDisplay values using enhanced for loop.\n\t");
	     for(int a : arr)
		     System.out.print( a + ", ");

	     }
	}

	Output :

				Display values, size taken from built-in length() function.
				78, 45, 12, 89, 56,

				Display values using enhanced for loop.
				78, 45, 12, 89, 56,

Java Labelled Loop

According to nested loop, if we put break statement in inner loop, compiler will jump out from inner loop and continue the outer loop again. What if we need to jump out from the outer loop using break statement given inside inner loop? The answer is, we should define lable along with colon(:) sign before loop. More info.

Advertisement