Java Programiing

LOOP IN JAVA

·

5 min read

Table of contents

No heading

No headings in the article.

LOOP

It is a special feature to execute a particular task of the program repeatedly when a certain condition is trueFor repetitive tasks or work we always go with the LOOP concept because loops will reduce the work

Mainly we have two loops in java which are

  • For loop

  • While Loop

FOR LOOP: when you know exactly how many times a particular task to be executed when using for loop. The loop system allows us to execute a

block of statements several numbers

of times depending on certain conditions .for loop is a kind of loop in which we give an initialization statement, test expression and update statement that can be written in one line.


for (initializationStatement; test_expression; updateStatement) {
// Statements to be executed till test_expression is true

Explanation :

A. Initialization - used to initialize your loop control variables. This statement is executed first and only once.

B. Test condition - this condition is checked every time we enter the loop. statements inside the loop are executed till this condition is evaluated to be true. As soon as the condition is evaluated to be false, the loop terminates and then the first statement after for loop will be executed next.

c. updation - this statement updates the loop control variable after every execution of statements inside loop. After updation, again test condition is checked. If that comes true, the loop executes and the process repeats. And if condition is false, the loop terminates.

Example code:

public static void main(String[] args) {
for(int i = 0; i < 3; i++) {
System.out.print("Inside for loop : ");
System.out.println(i);
}
System.out.println("Done");
}
/*
output
Inside for loop : 0 
Inside for loop: 1 
Inside for loop: 2
Done
*/

EXPLAINATION:

Step 1 Printing the first three elements using for loop.

Step 3: Then we need to give the condition as print the first 3 numbers so we are writing the condition like i < 3; which will print the first 3 numbers starting from 0.

Step 4: After completing one iteration we need to increment/decrement the process. So that it will loop through all the items until the condition becomes false.

Step 5: At each iteration i =0,1,2, it will come inside the for loop and it will print the output.

Step 6: Whenever the condition is false i.e, i = 3 which means 3 < 3, it will break out of the for loop and exit the program.

In for loop, it's not compulsory to write all three statement i.e initialization Statement , test_expression and an update. Statement. we can skip one or more of them (even all three)

The above code can write as


public static void main(String[] args) {
int i = 1; //initialization is done outside the for loop
for(; i < =5; i++) {
System.out.println(i);
}
}
/*
OUTPUT
1
2
3
4
5

Or

public static void main(String[] args) {
int i = 1; //initialization is done outside the for loop
for(; i < =5; ) {
System.out.println(i);
i++; // update Statement written here
}
}
/*
OUTPUT
1
2
3
4
5

Variation of for loop

.The three expressions inside for loop are optional. that means they can be omitted as per requirement.

Example code 1: initialization part removed

public static void main(String[] args) {
int i = 0;
for( ; i < 3; i++) {
System.out.println(i);
}
}
// output:
// 0
// 1
// 2

Example code 2: updation part removed

public static void main(String[] args) {
for(int i = 0; i < 3; ) {
System.out.println(i);
i++;
}
}
/*
Output:
0
1
2

Example code3: condition expression removed thus making our loop infinite

public static void main(String[] args) {
for(int i = 0; ; i++) {
System.out.println(i);
}
}

Example code4: we can remove all three expressions thus forming an infinite loop

public static void main(String[] args) {
for( ; ; ) {
System.out.print("Inside for loop");
}
}

. Multiple statements inside for loop

We can initialize multiple variables and have multiple conditions update statements inside a for a loop. We can separate multiple statements using commas, but not for conditions, They need to be combined using logical operators

Example code:

public static void main(String[] args) {
for(int i = 0, j = 4; i < 5 && j >= 0; i++, j--) {
System.out.println(i + " " + j);
}
}
/* output
0 4
1 3
2 2
3 1
4 0

break and continue

  1. break statement: THE break statement terminates the loop (for, while and do while loop ) immediately when it is encountered. As soon as a break is encountered inside a loop the loop terminates immediately. hence the statement after the loop will be executed next.

  2. continue statement: The continue statement skips some statements inside the loop. The continue statement is used with decision-making statements such as if-else. caution always update the counter in case of a while loop else the loop will never end.

while(test_expression){

// code

if(condition for break){
        break;

}

//codes
for (initializationStatement; test_expression; updateStatement) {
    // codes
    if (condition for break) {
    break;
    }
    //codes
    }

break

Example (using break inside for loop)

  public static void main(String[] args) {
    for(int i = 1; i < 10; i++) {
    System.out.println(i);
    if(i == 5) {
    break;
    }
    }
    }
/*
output:
1
1
3
4
5

. Example (using break inside while loop)

public static void main(String[] args) {
    int i = 1;
    while (i <= 10) {
    System.out.println(i);
    if(i==5)
    {
    break;
    }
    i++;
    }
    }
   /* Output:
    1
    2
    3
    4
    5

.Inner loop break

when there are two loops inside one another. Break from the innermost loop will just exit that loop

Example CODE 1:

public static void main(String[] args) {
    for (int i=1; i <=3; i++) {
    System.out.println(i);
    for (int j=1; j<= 5; j++)
    {
    System.out.println(“in”);
    if(j==1)
    {
    break;
    }
    }
    }
    }
   /* Output:
    1
    in…
    2
    in…
    3
    in…

Example code2:

public static void main(String[] args) {
    int i=1;
    while (i <=3) {
    System.out.println(i);
    int j=1;
    while (j <= 5)
    {
    System.out.println(“in”);
    if(j==1)
    {
    break;
    }
    j++;
    }
    i++;
    }
    }
/*
    output
1
in…
2
in…
3
in…