Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Loop in Java

  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • HTTP status codes List | Response Status Code Glossary
    HTTP status codes List | Response Status Code Glossary Important
  • What is Insurance with full details?
    What is Insurance with full details? | Life, Health, Car Insurance Important
  • Steps to Create a Servlet
    Steps to Create a Servlet Servlet
  • Python List
    Python List Python
  • Agile Methodology of Software Development
    Agile Methodology of Software Development Important
  • Difference between SQL and HQL
    Difference between SQL and HQL | Hql vs Sql performance Differences
  • Spring Initializr for Spring Boot Project
    Spring Initializr for Spring Boot Project Spring Boot

Loop in Java

Posted on August 31, 2021October 20, 2021 By admin No Comments on Loop in Java
Loop in Java

A Java loop is used to repeat a section of code several times. Sometimes we know how many iterations there are, and sometimes we don’t. For instance, if you want to display numbers from 1 to 10, you can use a loop, and if you want to print anything several times, you can also use a java loop.

The looping procedure is often divided into four steps: first, we set and initialize the counter, second we execute the statement in the loop, third the loop’s test condition, and fourth we increment the counter.

The Java loop not only reduces the number of similar statements that must be written, but it also minimizes the amount of code that must be written.

Table of Contents

  • Types of Loop in Java
  • While Loop in Java
    • Syntax of while Loop
    • Example of while Loop
  • Do-while Loop
    • Syntax of do-while Loop
    • Example of do-while Loop
  • For Loop in Java
    • Syntax of for Loop
    • Example of for Loop
  • Loop Control Statements in Java

Types of Loop in Java

There are mainly three basic types of loops in Java which are as follows:

  • While loop
  • Do-while loop
  • for loop

While Loop in Java

The while loop runs a block of code until the specified condition is satisfied. If you use a boolean type parameter in a while loop, you will get a compile time error. You should use a while loop if you don’t know the number of iterations ahead of time.

Syntax of while Loop


while (expression) {
  // Statements (Code to be executed)
}

Example of while Loop

Here is an example of a while loop in which the number from 1 to 10 is displayed with a newline. We initialize the counter outside the body, then verify the condition inside the parenthesis, and update the counter inside the loop.


public class WhileLoopDemo {
  public static void main(String[] args) {
    int count = 1;
    while (count <= 10) {
      System.out.println(count);
      count++;
    }
  }
}

Output:
1
2
3
4
5
6
7
8
9
10

Do-while Loop

At the bottom of the loop, the do-while checks its expression. Use a do-while loop if the number of iterations isn’t fixed and you want to run the loop body at least once.

Even if the condition is false, the control flow will arrive to the loop body in do-while.

Syntax of do-while Loop


do {
  // Statements (Code to be executed)
} while (expression);

Example of do-while Loop


public class DoWhileLoopDemo {
  public static void main(String[] args) {
    int count = 1;
    do {
      System.out.println(count);
      count++;
    } while (count <= 10);
  }
}

Output:
1
2
3
4
5
6
7
8
9
10

For Loop in Java

The for loop is used to go over a set of data. If you know how many times you want to iterate a section of code, loop is the way to go.

The nice thing about for loop is that you can use it to define numerous initialization variables, conditions, and counters.

Syntax of for Loop


for (initialization; condition; increment/decrement) {
  // Statements (Code to be executed)
}

In the above syntax, you can see that for loop has four parts:

  • initialization : The value we set to the counter at the start of the loop.
  • condition : This part determines whether or not the counter meets the condition. If the condition is satisfied, the loop will continue; otherwise, control will exit the loop.
  • increment/decrement : The counter rises or decreases by the value we provide in this section..
  • Statements : This is the section of the code that is looped around.

Example of for Loop


public class ForLoopDemo {
  public static void main(String[] args) {
    int count;
    for (count = 1; count <= 10; count++) {
      System.out.println(count);
    }
  }
}

Output:
1
2
3
4
5
6
7
8
9
10

Loop Control Statements in Java

The loop control statement changes the flow of the loop’s regular sequence of execution. For example, the loop may skip an iteration and control could leave the loop.

Following control statements are available in Java:

  • break statement: It breaks the loop and control comes out of the loop.
  • continue statement : It skips the current iteration of the loop.

In most situations, break and continue statements with an if condition are used in everyday programming. If we wish to break the loop after five iterations, we would use an if condition and a similar continue statement. In the switch case, we can also use the break statement to exit the switch block.

In Conclusion, I hope you gained a fundamental understanding of loops in Java via the use of syntax and examples. You may now tailor the loop to your own circumstance or demand.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java Tags:Boolean, break, continue, Do-While Loop, For Loop, Loop, While Loop

Post navigation

Previous Post: Java Switch Case
Next Post: What is Array in Java? Types of array in Java

More Related Articles

Importance of Thread Synchronization in Java Importance of Thread Synchronization in Java Java
Iterate Map in Java Iterate Map in Java Java
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
Lambda Expression in Java 8 | Functional Interface | Example Lambda Expression in Java 8 | Functional Interface | Example Java
Difference between Interface and Abstract class Difference between Interface and Abstract class Differences
Difference between JPanel and JFrame Difference between JPanel and JFrame Differences

Related Posts

  • Importance of Thread Synchronization in Java
    Importance of Thread Synchronization in Java Java
  • Iterate Map in Java
    Iterate Map in Java Java
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
    Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
  • Lambda Expression in Java 8 | Functional Interface | Example
    Lambda Expression in Java 8 | Functional Interface | Example Java
  • Difference between Interface and Abstract class
    Difference between Interface and Abstract class Differences
  • Difference between JPanel and JFrame
    Difference between JPanel and JFrame Differences

Leave a Reply Cancel reply

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

  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • HTTP status codes List | Response Status Code Glossary
    HTTP status codes List | Response Status Code Glossary Important
  • What is Insurance with full details?
    What is Insurance with full details? | Life, Health, Car Insurance Important
  • Steps to Create a Servlet
    Steps to Create a Servlet Servlet
  • Python List
    Python List Python
  • Agile Methodology of Software Development
    Agile Methodology of Software Development Important
  • Difference between SQL and HQL
    Difference between SQL and HQL | Hql vs Sql performance Differences
  • Spring Initializr for Spring Boot Project
    Spring Initializr for Spring Boot Project Spring Boot
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved