Learn Java Basics
About Lesson

A loop is a programming construct that allows you to repeat a block of code multiple times. Here is an example of a for loop in Java:

for (int i = 1; i <= 5; i++)
{
System.out.println("The value of i is " + i);
}

This loop will execute the code block five times, because the loop condition is set to i <= 5. The loop variable i is initialized to 1, incremented by 1 at the end of each iteration, and the loop will terminate when i becomes greater than 5.

Each time the loop iterates, the code block will print out the value of i. So the output of this loop would be:

The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5

This is just one example of a loop, and there are many other types of loops available in Java, as explained in my previous answer. Loops are a fundamental building block of programming, and they allow you to write concise and efficient code that can perform repetitive tasks.

Types of Loops

there are four main types of loops available, each with its own specific use cases. These are:

  1. for loop: The for loop is the most commonly used loop in Java, and it is used when you know the number of times you want to execute a block of code. The syntax for a for loop is:
for (initialization; condition; increment/decrement)
{
// code block to be executed
}

Here, the initialization statement is executed only once at the beginning of the loop. The condition is evaluated at the beginning of each iteration, and if it is true, the code block is executed. Finally, the increment/decrement statement is executed at the end of each iteration.

  1. while loop: The while loop is used when you don’t know the number of iterations you want to execute beforehand. The syntax for a while loop is:
while (condition)
{
// code block to be executed
}

Here, the condition is evaluated at the beginning of each iteration, and if it is true, the code block is executed. The loop continues until the condition becomes false.

  1. do-while loop: The do-while loop is similar to the while loop, but the condition is evaluated at the end of each iteration. This means that the code block is executed at least once, regardless of the condition. The syntax for a do-while loop is:
do
{
// code block to be executed
} while (condition);

Here, the condition is evaluated at the end of each iteration, and if it is true, the loop continues. If it is false, the loop terminates.

  1. enhanced for loop (for-each loop): The enhanced for loop is used to iterate over arrays or collections. The syntax for an enhanced for loop is:
for (type variable : array/collection)
{
// code block to be executed
}

Here, the type is the data type of the elements in the array or collection, and variable is a variable that represents each element in turn. The loop iterates over each element in the array or collection, and the code block is executed for each element.

Parts of Loop

There are typically three parts of a loop:

  1. Initialization: This is where you set up any variables or conditions needed before the loop begins. For example, you might initialize a counter variable to zero before starting a loop that will increment the counter.

  2. Condition: This is the test that is evaluated before each iteration of the loop. If the condition is true, the loop body will be executed; if it’s false, the loop will terminate. For example, you might use a condition to test whether the counter variable has reached a certain value.

  3. Update: This is where you modify the variables or conditions used in the loop. For example, you might increment the counter variable at the end of each iteration of the loop.

for (initialization; condition; update)
{
// code to be executed
}

There are also other types of loops in Java such as while and do-while loops, but they also have similar parts to a loop: initialization, condition, and iteration.

 


Join the conversation