Study Smart, Not Hard. - StudyMuch

Loops in Java

Published by StudyMuch on

Loops in Java studymuch.in

Loops in Java

In this article you will learn about the Loops in Java. Here you read definition of Loops, and all the types of loops in Java with programming examples. So, let’s start the learning.

What is Loop?

In java programing language, loop is used to iterate a part of the program several times. Loops are iterative statements. These statements help the developers to iterate the program codes, or a group of codes runs multiple times (as per the need).

Types of Loops

Primarily, there are three types of loops in Java. Java provides three ways for executing the loops. While all the ways provided similar basic functionality, they different in their syntax and condition checking time.

  1. for loop
  2. while loop
  3. do-while loop

given below definition of all the types of loops with programming examples.

for loop

In java, for loop is a control flow statement that iterates a part of the program multiple times. for loop provides a concise way of writing the loop structure.

Programming example of for loop;

public class table {
public static void main(String[] args) {
    int n=8;
   for (int i=1; i<=10; i++){
System.out.printf(“%d x %d = %d\n”,n , i , n*i);}
}
}

Output;

for loops output studymuch

while loop

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

while loop is used to repeat a section of code an unknown number of times until a specific condition is met.

Programming example of while loop;

public class example {
  public static void main(String[] args) {
     //while loop
     System.out.println("Print no. 1 to 5");
     int a=1;
     while (a<=5){
        System.out.println(a);
        a++;
       }
   }
}

Output;

while loop output studymuch

do-while loop

A do-while loop is an exit-controlled loop, do-while loop similar to while loop with only difference that it checks for condition after executing the statements. The condition is always checked at the end of the do-while loop.

Programming examples of do-while loop;

public class tables {
  public static void main(String[] args) {
    //do-while loop
    System.out.println("Print no. 1 to 6");
    int a=1;
     do {
          System.out.println(a);
            a++;
        } while (a<=6);
      }
    }

Output;

do-while loop output studymuch



Difference between while and do-while loops

while

do-while

It is a looping construct that will executed only if the test condition is true.
It is a looping construct that will executed at least once even if the test condition is false.
It is an entry-controlled loop.
It is an exit-controlled loop.
It is generally used for implementing common looping situations. It is typically used for implementing menu-based programs.

So, in this article you learned all about the Loop in Java, what is loops, types of loops with programming examples. And I hope you guys, you read this tutorial better and learn something new. If you have any doubt, you can ask in the comment section.

Read Also


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *