Java Loops

Loops are used to execute a set of functionality again and again till the condition turn into accurate. In java, there are three types of loops:

  • for loop
  • while loop
  • do-while loop

The for loop statement is the most used, but while and do-while have their uses as well.

for loop

In java for loop is used to repeat the part of the program many times. If the number of repetition is fixed, then for loop is use.

while loop

In java while loop is used to repeat the part of the program many times. If the number of repetition is not fixed, then while loop is use.

do-while loop

In java do-while loop is used to repeat the part of the program many times. If the number of repetition is not fixed and need to execute loop at least once.

For Loop

In java for loop is used to repeat the part of the program many times. If the number of repetition is fixed, then for loop is use. For loop is recommended for iterating on objects like arrays and lists that can be counted.

Syntax

for(initialization; condition ; increment/decrement)

{

statement(s);

}

Flowchart
  1. Initialization

    An initialization expression that sets the initial value of the counter used by this loop. It executes only once when the loop starts.

  2. Condition

    It execute each time to test the condition. In condition of the loop, as long as this condition is evaluated to true, the loop will continue executing.

  3. Statement

    In statement, it executed every time until the second condition is false.

  4. Increment/Decrement
Example

class ForExample {

public static void main(String[] args) {

//Code of Java for loop

for(int i=1;i<=8;i++){

System.out.println(i);

}

}

}

Output

1

2

3

4

5

6

7

8

Nested for Loop

When for loop is inside of another loop, then it is called nested for loop. Whenever outer loop executes inner loop also executes completely.

Example

class NestedForloop {

public static void main(String[] args) {
//loop of i

for(int i=A;i<=C;i++){                                         //loop of j

for(int j=A;j<=C;j++){

System.out.println(i+” “+j);

}                                                                            //end of i

}                                                                            //end of j

}

}

Output

A A

A B

A C

B A

B B

B C

C A

C B

C C

Pyramid Example

class PyramidExample {

public static void main(String[] args) {

for(int i=1;i<=6;i++){

for(int j=1;j<=i;j++){

System.out.print(“1 “);

}

System.out.println();                                          //new line

}

}

}

Output

1

1 1

1 1 1

1 1 1 1

1 1 1 1 1

1 1 1 1 1 1

For-each Loop

For-each loop is used to navigate array. It is easy to use than simple for loop. It does not need to increment value and use subscript notation.

It deals with components premise, not file. It returns component individually in the characterized variable.

Syntax

for(Type var:array){

//code to be executed

}

Example

class ForEachExample {

public static void main(String[] args) {                                         //Declare an array

int arr[]={11,22,33,44,55};                                           //Print array using for-each loop

for(int i:arr){

System.out.println(i);

}

}

}

Output

11

22

33

44

55

Infinitive for Loop

Use of two semicolons ; ; in for loop is called infinitive for loop.

Syntax

for(; ;){

//code to be executed

}

Example

class ForExample {

public static void main(String[] args) {

//Using no condition in for loop

for(;;){

System.out.println(“infinitive for loop”);

}

}

}

Output

infinitive loop

infinitive loop

infinitive loop

infinitive loop

infinitive loop

ctrl+c

Ctrl+c is use to exit the program

Copyright 1999- Ducat Creative, All rights reserved.