Friday, February 5, 2021

Loops in java-AapKiApniSchool

In this blog we will discuss all 3 type of loop in java in details. We will also discuss Infinite loops also. We will also discuss why for loop better than while loop and difference between while and do while loop. We will go through enhanced for loop that is also known for for-each loop.

Table of content:

  1. What are loops?
  2. What are the 3 types of loops?
  3. What is loop syntax?
  4. What is difference between while loop and do while loop?
  5. WHY IS FOR loop better than while loop?
  6. Which for loop is faster?
  7. Which loop is faster in Java?
  8. Infinite loop
    1. Infinite for loop
    2. Infinite while loop
    3. Infinite do-while loop
  9. Enhanced for loop(for-each loop)

Loops in java:

Loops in Java is used to execute some set of instructions repeatedly when some are conditions.

Types of Loops:

  1. for loop
    1. Enhanced for loop
    2. Infinite for loop
  2. while loop
    1. Infinite while loop
  3. do while loop
    1. Infinite do while loop

For loop:

For loop is a short hand of writing loop. It consists initialization,condition and increament/decreapment in one line.

Syntax:

  for(initlozation;condition;increment/decreament)
{

}

Flow chart:

Initialization:

In Initialization we provide the initial value of variable to start loop. We can use already declared variable or we can create new variable.

Condition:

Before executing loop first condition value is checked if condition is true then only block of loop is executed else loop terminated. Condition is mainly used to terminate loop after a particular condition failed.

Increment/decrement:

After executing first iteration variable value is increment or decrement and again condition checked.

Enhanced For loop:

In java 5 we got enhancement of for loop. Enhanced for loop is providing a simple way for iterating collection or array. Enhanced for loop is used only when you want to iterate a array or collection in a sequential manner without knowing index. Enhanced for loop is also know is for-each loop. It increase readability. Best drawback of for each loop is when iterating any array or collection we can't skip any element or we can't traverse in reverse order.

for(data_type variable : array | collection){  
//body of for-each loop  
} 
package testjava;

import java.util.ArrayList;
import java.util.List;

public class TestLoop {

	public static void main(String[] args) {
		
		List list= new ArrayList<>();

		list.add("Sunil");
		list.add("Anil");
		list.add("Narendra");
		
		for(String x:list)
		{
			System.out.println(x);
		}
	}

}

Output:
Sunil
Anil
Narendra

Java Infinite for loop:

If you don't pass Initialization, condition and increment/decrement value in for loop then it will be infinite for loop.
for(;;)
{}

While loop:

While loop first check condition, if condition is true then it will execute the body of loop otherwise terminate loop. While loop is used when iteration is not fixed. While loop also known for Entry check loop.

   while(condition)
   {
      //loop body
   }
  

Java Infinitive While Loop:

if you pass true in condition then it will be infinite while loop. It will never terminate.
 
    package testjava;

public class TestLoop {

	public static void main(String[] args) {
	
		while(true)
		{
			System.out.println("number ");
		
		}
	}

}

  
Note: If you want to stop infinite loop then press Ctrl+C

do while:

do while loop first execute the body after that check condition. If condition is true then again it will execute iteration. do while loop execute at least one time if condition is true or not. In do while loop for first execution condition is not checked.
    
<1 pre=""> package testjava; public class TestLoop { public static void main(String[] args) { int i=1; do { System.out.println("Number is "+i); }while(i<1); } }
<1 pre="">
<1 pre="">Output:
<1 pre="">
<1 pre="">Number is 1

Java Infinitive do-while Loop

If you pass true in the do-while loop, it will be infinitive do-while loop.

Syntax:

do{  
//code to be executed  
}while(true);  
<1 pre="" style="background-color: powderblue;">package testjava;

public class TestLoop {

	public static void main(String[] args) {
	
		do {
			System.out.println("Number");
		}while(true);
	}

}

Note: If you want to stop infinite loop then press Ctrl+C

What is difference between while loop and do while loop?

While loop first check condition if condition is true then execute the body of loop but do-while loop first execute the body and then check condition if condition is true then it will condition execute loop. Do-While loop execute at least one time if condition is false or true. While loop can execute 0-n time but Do-While loop execute 1-n times.

WHY IS FOR loop better than while loop?

Performance of for loop and while loop is same. The advantage of a for loop is that it's harder to accidentally do an infinite loopOr rather, it's more obvious when you do one because you generally put the loop var in the initial statement. If we know the iteration then we should use for loop. If we don't know the iteration then we should use while loop. For example reading a file. 

No comments:

Post a Comment

Featured Post

Interface in java-AapKiApniSchool

Interface in java:  interface is a blueprint of class in java that is used to define contracts. It can contain static constant and abstract ...

Popular Posts