Loop is used in programming to repeat a specific block of code until certain condition is met. Put simply, loop enables your program to execute the block of code repeatedly. Imagine you need to print a sentence 50 times on your screen. Well, you can do it by using print statement 50 times (without using loops). How about you need to print a sentence one million times? So, you need to use loops.

In Java, there are four important types of loops as following:

  • for loop
  • while loop
  • do while loop
  • for each loop

for Loop Statement: “for” Loop is one of the looping statement in java programming. “for” Loop checks the contrition and executes the set of the statements repeatedly until the condition is true. “for” Loop contain the following statements such as “Initialization” , “Condition” and “Increment/Decrement” statement. 

infinite for Loop: If the test expression is never false, “for” loop will run forever. This is called infinite for loop. Let’s take an example: 

 

while Loop Statement: In java “while” is iteration statements like for and do-while. It is also called as Loop Control Statement. “while Statement” repeatedly executes the same set of instructions until a termination condition is met. “while” loop is Entry Controlled Loop because condition is check at the entrance. If initial condition is true then and then only control enters into the while loop body. 

Boolean Condition while Loop: We can put single variable as while condition but it must be of type boolean. thus the following statement will cause compile time failure – 

 

do-while Loop Statement: In java “do-while” loop body gets executed once whatever may be the condition but condition must be true if you need to execute body for second time. “do-while” Statement is Exit Controlled Loop because condition is check at the last moment. Irrespective of the condition, control enters into the do-while loop, after completion of body execution, condition is checked whether true/false. If condition is false then it will jump out of the loop. 

 

for each Loop Statement: The for each loop is generally used with a collection (e. g. array). By using the for each loop, you can access all elements of the collection without knowing the number of items in the collection. The example code below reads available font names from your computer and output them on the console window. 

 

Break Statement: Break Statements skips remaining statements and execute immediate statement after loop. Break statement is used whenever our condition is satisfied inside loop and we have to come outside the loop. Break statement is used to make looping statements more flexible and provides more power to it. Break Statement is used to break : for Loop | while Loop | do-while loop. 

Continue Statement: Continue Statement in Java is used to skip the part of loop. Unlike break statement it does not terminate the loop , instead it skips the remaining part of the loop and control again goes to check the condition again. Continue Statement can be used only in Loop Control Statements such as For Loop | While Loop | do-While Loop. 

 

That’s all about Loop in Java. Thanks a lot for reading…

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.