Loop is used in programming to repeat a specific block of code until a certain condition is met. Put simply, the 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 a 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, the “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 the condition is checked at the entrance. If the initial condition is true then and then only control enters into the while loop body.

Boolean Condition while Loop: We can put a single variable as a 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 the second time. “do-while” Statement is Exit Controlled Loop because the condition is checked at the last moment. Irrespective of the condition, control enters into the do-while loop, after completion of body execution, the condition is checked whether true/false. If the 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 an immediate statement after the loop. The break statement is used whenever our condition is satisfied inside the loop and we have to come outside the loop. The 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 the loop. Unlike the 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. 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…

The article was published on August 1, 2017 @ 10:45 AM

Leave a Comment