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, Looping statements in java (for, while, do..while)

  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • What is Noun
    What is a Noun? 5 Types of Nouns with Examples, Noun diagram Grammar
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
    International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं? General Knowledge
  • Rabindranath Tagore Biography in Hindi, Poems, Birthday
    Rabindranath Tagore Biography in Hindi, Title of Gurudev Biography
  • Indian Navy Day
    Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है? General Knowledge
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge

Loop in Java, Looping statements in java (for, while, do..while)

Posted on August 31, 2021May 20, 2022 By GeekCer Education No Comments on Loop in Java, Looping statements in java (for, while, do..while)
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 Loops in Java
  • While Loop in Java
    • Syntax of while loop in Java
    • While loop program in java
  • Do-while Loop in Java
    • Do while loop syntax in java
    • Do-while loop program in java
  • For Loop in Java
    • Syntax of for loop in Java
    • For loop program in java
  • Loop Control Statements in Java

Types of Loops 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 in Java


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

While loop program in java

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 in Java

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.

Do while loop syntax in java


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

Do-while loop program in java


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 in Java


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.

For loop program in java


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, for loops in java, Loop, What are loops in java, What is a loop in java?, While Loop

Post navigation

Previous Post: Java Switch Case : Switch fall-through, default, break, examples
Next Post: What is Array in Java? Types of array in Java

More Related Articles

Concurrent collections in Java, details, advantages, examples Concurrent collections in Java, details, advantages, examples Java
Multithreading in java Multithreading in java, Thread, Runnable | Life Cycle of Thread Java
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences
Fork/Join Framework in Java | RecursiveTask, RecursiveAction Fork/Join Framework in Java | RecursiveTask, RecursiveAction Java
Java Comments Java Comments : Types of java comments, Syntax & Examples Java
Importance of Thread Synchronization in Java Thread Synchronization in Java | Synchronized Method & Block Java

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
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • 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