Important Points to Keep in Mind while Using Loops in Java

ADMEC Multimedia > Web Development > Important Points to Keep in Mind while Using Loops in Java

Loops are used in any programming languages when we require to execute the statements on a certain condition multiple time. Loop facilitates iteration of the statements till the specified condition becomes false.  Loops in Java is our topic on which we are going to discuss today. One should understand the core concept of loops while attaining trainingof Java courses in Delhi to become a master of this programming language. So, lets dive in depth to have an understanding on loops in Java. Basically Java contains three kinds of loops which have given below:

  • for loop
  • while loop
  • do while loop

Working with Loops in Java

Let us discuss the working of loops and also have a look on a few points which should be remembered while using loops. So let us start with for loop.

For loop

It is the loop which execute the statements when the condition is true. Let us see the flow and syntax of for loop. Syntax of For loopThis is the basic syntax of for loop

for(; ;)
//Generally we use its syntax as
for( initialization; condition; increment/decrement )

Control Flow of for loop is based on the condition whether it’s false or true.Flow of for loop:-

Example of for loop:

for( int i = 1; i < 10; i += 2 )

9In version 5 of Java, new form of for loop is introduced known as foreach loop or enhanced for loop. Example of enhanced for loop:

for(int i:array)

Control flow of foreach loop:

This is used to traverse the array. It does not have the requirement of indexing of array. It is useful only when we want to traverse the array. It does not allow the modification in array elements. In this loop, we take a variable of same data type like array. It means if the array contains the data of int type then you are required to use the same type of data to store the value of the array. We can use for loop with labels also. It is useful when we are working with nested for loops and can use break and continue statement easily without any confusion. 

For Example:

aa: for (int m = 1; m <= 3; m++) {
    if (m == 1)
    bb: for (int l = 1; l <= 3; l++) {
        if (m == 2 && l == 2) {
            break aa;
        System.out.println(i + " " + j);

In example, aa and bb is the label which is used to add a label to for loop for effectively use of continue and break within loop.

Points to Remember While using For Loop in Java:

Condition must be correct. It means condition must be written carefully. Sometimes we write a condition in for which can not become false and it results into an infinite loop. So to avoid this type of situation, you should apply the condition in for loop which becomes false at a specific time.   

while loop

It is a loop which is used to iterate the statements till condition becomes false. We can use while loop when we don’t know the number of iterations. Syntax of while loop:

while( condition/ boolean expression )

Control flow of while loop:

Example of while loop

int i=0;
Points to Remember While using While Loop in Java:

Must initialize the variable before using it in while loop. Must write a statement which results into termination of the loop.

do while loop

It is a loop which is used to execute the set of statements at least once without checking the condition and then check the condition if the condition is true then it will execute those statements again till condition become false, otherwise terminate. Syntax of do while loop

     increment/decrement (++ or --);

Control flow of do while loop:

Example of do while loop:

     System.out.println("Hello world !");
Points to Remember While using do while loop in Java:

Do not use this loop when your requirement is the execution of statements after check the condition. In the blog we have read about some essential points that one should keep in mind while using loops in Java. Foreach loop is introduced in version 5 of Java. Many more things are there which are introduced with new versions.

These are the loops which are available in Java. And we understood that Looping in Java enables the implementation of a set of functions frequently whereas some condition evaluates to true.Hope you’ll get support from this blog in understanding loops in Java.To read more such kinds of blogs do visit our Java blogs section.

Related Posts

Leave a Reply

Copy link
Powered by Social Snap