Loop Control in Java

Hello readers, in today’s topic we will discuss loop control in Java. Previously we also discussed various important topics of Java like Decision making in Java, Operator in Java, Keywords in Java, and many more. So without wasting the time let’s dive into topic:

Loop Control in java

Loops in Java are important features that facilitate the execution of a block of code repeatedly. There are many situations when we need to execute a block of code or a set of instructions several times. In loop control statements are executed sequentially one by one according to the given condition.

Sometimes we want to execute a few sets of instructions for our program over and over again. For example, print 1 to 1000, print a multiplication table of 9, etc.

Loops make it easy for users to tell the computer that a given set of instructions needs to be executed repeatedly.  Here the first statement within a function is executed first, followed by the second, third, and so on.

Java as well as other programming languages provide various types of control structures which get benefited from more complicated execution.

So Java provides three types of loops for executing the programs. All the loops have a similar type of functionality but they have to differ in syntax and condition checking.

  • While loop
  • Do-while loop
  • For loop

while loop

While loop in java is a control flow statement which used to execute a block of code or group of statements repeatedly based on the given conditions. Here the condition is tested first and then the body of the while-loop is executed.

Syntax:-

while (boolean condition)
{
   Body of the loop
   loop statements...
}

First, it checks the condition. If the given condition is evaluated to true, then the body of the loop is executed otherwise it skips the body of the loop and gets terminated. Generally, within the loop body, it contains an update value (increment/decrement). This updated variable is responsible for the next iteration.

When the loop get terminates then it indicates the end of its life cycle. If the condition never becomes false, then the while loop keeps getting executed repeatedly such a type of loop is known as an infinite loop.

Example:-

// Example of while loop in Java by GeektoCode
public class GeektoCode {
        public static void main(String args[]){
            int i = 1;
             while (i < 10) {
                System.out.println("Yes " + i + " is less than 10");
                i++;
            }
            System.out.println("Terminating of loop");
        }
}

OUTPUT:-

Yes 1 is less than 10
Yes 2 is less than 10
Yes 3 is less than 10
Yes 4 is less than 10
Yes 5 is less than 10
Yes 6 is less than 10
Yes 7 is less than 10
Yes 8 is less than 10
Yes 9 is less than 10
Terminating of loop

do while loop

do while loop in java is quite similar to while loop. Here the difference is that it checks the given condition after executing the loop body or end of the loop body.

Syntax:-

do
{
	loop body
    	statements...
}
while (condition);

Here the execution of the loop body is performed first because at the beginning there is no checking of any condition. Then after the execution of the loop body update of the variable value is occurred, then finally the given condition is checked if that is true or false. Here if the condition is true then the next iteration of the loop starts. Here also when the given condition becomes false, the loop gets terminated.

Note:- do-while loop will execute its body at least once before the checking of any condition.

Example:-

// Example of do while loop in Java by GeektoCode
public class GeektoCode {
        public static void main(String args[]){
            int i=1;
             do{
                System.out.println("Yes " + i + " is less than 1");
                i++;
            }while (i<1);
            System.out.println("Terminating of for loop");
        }
}

OUTPUT:-

Yes 1 is less than 10
Terminating of for loop

In the above example, we assign a value to the variable named ‘i’ i.e. 1 and our condition is “i is less than 1”. So when the execution starts without checking the given condition the loop body is executed and then the condition is checked with the updated value.

for loop

For loop in java is also a control flow statement which used to execute a sequence of statements multiple times repeatedly based on the given condition. Here first the initialization is occurred followed by the loop body being executed according to the condition and finally, the initialization value is updated i.e. increment/decrement. All these above operations are performed in one line.

Syntax:-

for (initialize ; condition;  increment/decrement)
{
     body of loop
}

Here, we can initialize the variable by assigning a value. Then testing the given condition. Remember that it must return a boolean value. If the given condition is true, then the statements in the loop body are executed. And then the updating of the variable for the next iteration by using the increment/decrement operator.

Example:-

// Example of for loop in Java by GeektoCode
public class GeektoCode {
        public static void main(String args[]){
            int i;
             for(i = 1; i < 10 ; i++){
                System.out.println("Yes " + i + " is less than 10");
            }
            System.out.println("Terminating of for loop");
        }
}

OUTPUT:-

Yes 1 is less than 10
Yes 2 is less than 10
Yes 3 is less than 10
Yes 4 is less than 10
Yes 5 is less than 10
Yes 6 is less than 10
Yes 7 is less than 10
Yes 8 is less than 10
Yes 9 is less than 10
Terminating of for loop

So this is all about loop control in Java. I hope this is really helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *