Table of Contents
Loops in JAVA
Loops or iterations in programming languages is a feature where in you can execute a few lines of code several times as long as the loop entry conditions are met. It facilitates the process of revisiting a segment of instructions, statements, or functions repeatedly. When entering the loop after one iteration, the loop control variable must fulfil certain loop conditions such that the next iteration is executable. If the loop condition is not met, the loop terminates and code following the loop is executed. Loop conditions can either be placed at the start of the loop or at the end depending upon the type of loop you are using. There are three types of loops that Java offers:
- For Loop – For Loops are the most common types of loops in Java. When the number of iterations is fixed or bounded by a certain variable, it is best to use for loops.
- While Loop – This loop is also used to iterate a part of the program several times. However, while loops do not require a value for the number of times the loop will execute.
- Do- While Loop – The do while loop is also used to iterate over a certain part of your program. Since the loop already has a while component, the number of iterations is not fixed. However, such kind of loops make sure that compiler enters and executes the loop at least once before termination.
For Loops
When the number of iterations is fixed, for loops are preferred in Java. The basic structure of for loops is similar to C/C++. The loop is controlled by a variable which is known as the Loop Control Variable (LCV). This variable is initialized and incremented or decremented after every iteration. The loop condition is determined by the current value of the LCV. A simple for loop can be broken down into four parts as follows:
- Initialization: The LCV can either be initialized outside the loop or inside the loop declaration. Either way, this marks the start of the first iteration.
- Condition: This is the second check that takes place. For the first iteration. after the initialization, this condition is checked. For consecutive iteration, the last value of the LCV at the end of the loop is checked.
- Statement: This is the body of the loop.
- Increment/Decrement depends on the code.
Example
…. for (int i = 0, i< 10; i++) { //Body of the loop. This loop will execute 10 times. System.out.println (i); } …..
OUTPUT
0
1
2
3
4
5
6
7
8
9
While Loop
When the number of iterations is not fixed, while loops are generally used. These loops continue iterating until the loop condition is met. For this reason, it is called the entry control loop.
Example
class Demo { public static void main (String args []) { int x = 1; while (x <= 4) { System.out.println (“The value of x:” + x); x++ } } }
OUTPUT
The value of x: 1
The value of x: 2
The value of x: 3
The value of x: 4
Do-While Loops
These loops are similar to While loops with the exception that condition checking happens after execution of the body of the loop. This means no matter what the value of the LCV, do-while loops will always iterate once.
Example
class Demo { public static void main (String args []) { int x = 5 do { System.out.println (“The Value of x is” + x); x++ } while (x < 0); }
OUTPUT
The Value of x is 5
0 Comments