A "loop" is an important topic in programming because it allows us to reduce the amount of time we spend doing the same thing over and over. Let us say you want to print your name five times. Most people just write their names in the print function and run. Yes, that's correct. But what if I say that to print your name 100 times, you may copy and paste 100 times? for a total of 10,000 times? No, it seems difficult for you to do that. Here, the loops help you finish this task.

Don't misunderstand, loops are not only for printing, In this blog, we will also be solving a few problems and giving you an idea about loops.

For Loop:

This loop is used to execute a block of code a specific number of times. It has a counter variable that starts at a certain value and increments or decrements by a specified amount each time the loop iterates. The for loop is often used when the number of iterations is known in advance.

Syntax:

for ( initialize variable ; condition ; inc/dec){

//code

}

Example:

for (int i = 0; i < 5; i++) {

System.out.println("Hello World!");

}

While Loop:

This loop is used to execute a block of code as long as a certain condition is true. The condition is checked before each iteration, and if it is false, the loop exits. The while loop is often used when the number of iterations is not known in advance.

syntax:

initialize var;

while (condition){

//code

inc/dec

}

Example:

int counter = 0;

while (counter < 5) {

System.out.println("Hello World!");

counter++;

}

do...while loop:

This loop is similar to the while loop, but the condition is checked after each iteration. This means that the code inside the loop is guaranteed to execute at least once.

syntax:

initialize var;

do{

//code

inc/dec

}while( condition);

Example:

int counter = 0;

do {

System.out.println("Hello World!"); counter++;

} while (counter < 5);

For each loop:

This loop is used to iterate over the elements of an array or a collection, such as a list or a set. It is a simplified version of the for loop, and it makes it easy to iterate over all the elements of a collection without having to worry about the index.

syntax:

for ( int var : var2)

{
code

}

Problem-solving:

Question 1: Write a program that uses a for loop to print the numbers 1 to 10.

Code:

public class Question1{

public static void main(String[ ] args){

for ( int i = 1; i<=10; i++){

System.out.println(i);

}

}

Here, the variable has initialized with the value of 1, for every iteration we will be printing the value of i variable in the output section. While gives the output as:

1 2 3 4 5 6 7 8 9 10

Question 2: Write a program that uses a while loop to print the numbers 10 to 1.

Code:

public class Question2{

public static void main(String[ ] args){

int i=10;

while(i>=0){

System.out.println(i);

i--;

}

}

Here, we had declared a variable as 10, but instead of printing it from 1 to 10, we had incremented it. But, here we need to print it from 10 to 1. So, we have to decrement from 10 to 1 for every iteration loop completes.

Question 3: Write a program that uses a do-while loop to print the even numbers between 0 and 10.

public class Question3{

public static void main(String[ ] args){

int var = 0;

do{

System.out.println(i+2);

i++;

}while(i<=10);

Question 4: Write a program that uses a for loop to find the sum of the first 100 natural numbers.

public class Question4{

public static void main(String[ ] args){

int sum =0;

for(int i=0; i<=100; i++){ sum+=i;

}

System.out.println(sum);

}

}

It prints the sum of every value of i for every iteration. After completion of the loop, it comes to the next line in which it prints the sum of iterations.

I believe these examples helped you to understand the loops in Java. I strongly recommend you solve the problems on loops on platforms like hackerrank, CodeChef, etc.

Did you find this article valuable?

Support Manjunath Irukulla by becoming a sponsor. Any amount is appreciated!