Loops in java

Aap Ki Apni School
2 min readFeb 7, 2021

Hello Guys,

In this blog we will discuss all 3 type of loop in java in details. We will also discuss Infinite loops,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. This blog will give you brief knowledge of loops in java.

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. Infinite loop
  7. 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
  2. Enhanced for loop
  3. Infinite loop
  4. while loop
  5. 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)

{

//loop body

}

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

More details

--

--

Aap Ki Apni School

I am a software developer. I have been working in IT since 5 years. I am interested in learning new things.