Java Basics:Loop statements in Java
Loops in Java
In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in Java.
- for loop
- while loop
- do-while loop

Java For Loop vs While Loop vs Do While Loop
Comparison | for loop | while loop | do while loop |
---|---|---|---|
Introduction | The Java for loop is a control flow statement that iterates a part of the programs multiple times. | The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition. | The Java do while loop is a control flow statement that executes a part of the programs at least once and the further execution depends upon the given boolean condition. |
When to use | If the number of iteration is fixed, it is recommended to use for loop. | If the number of iteration is not fixed, it is recommended to use while loop. | If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use the do-while loop. |
Syntax | for(init;condition;incr/decr){ // code to be executed } | while(condition){ //code to be executed } | do{ //code to be executed }while(condition); |
Example | //for loop for(int i=1;i<=10;i++){ System.out.println(i); } | //while loop int i=1; while(i<=10){ System.out.println(i); i++; } | //do-while loop int i=1; do{ System.out.println(i); i++; }while(i<=10); |
Syntax for infinitive loop | for(;;){ //code to be executed } | while(true){ //code to be executed } | do{ //code to be executed }while(true); |
Java For Loop
The Java for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loops in java.
- Simple For Loop
- For-each or Enhanced For Loop
- Labeled For Loop
Java Simple For Loop
A simple for loop is the same as C/C++. We can initialize the variable, check condition and increment/decrement value. It consists of four parts:
- Initialization: It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition.
- Condition: It is the second condition which is executed each time to test the condition of the loop. It continues execution until the condition is false. It must return boolean value either true or false. It is an optional condition.
- Statement: The statement of the loop is executed each time until the second condition is false.
- Increment/Decrement: It increments or decrements the variable value. It is an optional condition.
Syntax:
Flowchart:

Example:
Output:
1 2 3 4 5 6 7 8 9 10
Java Nested For Loop
If we have a for loop inside the another loop, it is known as nested for loop. The inner loop executes completely whenever outer loop executes.
Example:
Output:
1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3
Pyramid Example 1:
Output:
* * * * * * * * * * * * * * *
Pyramid Example 2:
Output:
* * * * * * * * * * * * * * * * * * * * *
Java for-each Loop
The for-each loop is used to traverse array or collection in java. It is easier to use than simple for loop because we don't need to increment value and use subscript notation.
It works on elements basis not index. It returns element one by one in the defined variable.
Syntax:
Example:
Output:
12 23 44 56 78
Java Labeled For Loop
We can have a name of each Java for loop. To do so, we use label before the for loop. It is useful if we have nested for loop so that we can break/continue specific for loop.
Usually, break and continue keywords breaks/continues the innermost for loop only.
Syntax:
Example:
Output:
1 1 1 2 1 3 2 1
If you use break bb;, it will break inner loop only which is the default behavior of any loop.
Output:
1 1 1 2 1 3 2 1 3 1 3 2 3 3
Java Infinitive For Loop
If you use two semicolons ;; in the for loop, it will be infinitive for loop.
Syntax:
Example:
Output:
infinitive loop infinitive loop infinitive loop infinitive loop infinitive loop ctrl+c
Now, you need to press ctrl+c to exit from the program.
Java While Loop
The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.
Syntax:
Output:
1 2 3 4 5 6 7 8 9 10
Java Infinitive While Loop
If you pass true in the while loop, it will be infinitive while loop.
Syntax:
Example:
Output:
infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c
Now, you need to press ctrl+c to exit from the program.
Java do-while Loop
The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
The Java do-while loop is executed at least once because condition is checked after loop body.
Syntax
Example:
Output:
1 2 3 4 5 6 7 8 9 10
Java Infinitive do-while Loop
If you pass true in the do-while loop, it will be infinitive do-while loop.
Syntax:
Example:
Output:
infinitive do while loop infinitive do while loop infinitive do while loop ctrl+c
Now, you need to press ctrl+c to exit from the program.
___________________________
jump Statements
- jump: Java supports three jump statement: break, continue and return. These three statements transfer control to other part of the program.
- Break: In Java, break is majorly used for:
- Terminate a sequence in a switch statement (discussed above).
- To exit a loop.
- Used as a “civilized” form of goto.

Example:
Using break to exit a Loop
Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.
Output:i: 0 i: 1 i: 2 i: 3 i: 4 Loop complete.
Using break as a Form of Goto
Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses label. A Label is use to identifies a block of code.
Syntax:label: { statement1; statement2; statement3; . . }
Now, break statement can be use to jump out of target block.
Note: You cannot break to any label which is not defined for an enclosing block.
Syntax:break label;
Example:
Output:
Before the break. This is after second block.
- Continue: Sometimes it is useful to force an early iteration of a loop. That is, you might want to continue running the loop but stop processing the remainder of the code in its body for this particular iteration. This is, in effect, a goto just past the body of the loop, to the loop’s end. The continue statement performs such an action.
Example:Output:
1 3 5 7 9
- Return:The return statement is used to explicitly return from a method. That is, it causes a program control to transfer back to the caller of the method.
Example:Output:
Before the return.
- ______________-
Comments
Post a Comment