Home     Blog

Java Loops – For, While, and Do

Java supports three types of loops: For, While, and Do.

The Java For Loop

The Java for loop is a looping construct which continually executes a block of statements over range of values.

Java for Loop Syntax

The syntax of a for loop in Java is:

for (initialization; termination; increment) {
 statement
}

Example Java for Loop

Here is a Java for loop which prints the numbers 1 through 10.

for (int loopvar = 1; loopvar <= 10 ; loopvar++) {
 System.out.println(loopvar);
 }
Java for Loop Java Loops   For, While, and Do

The Java While Loop

A Java while loop is a looping construct which continually executes a block of statements while a condition remains true.

Java while Loop Syntax

The syntax of a while loop in Java is:


while (expression) {
statement
}

Example Java while Loop

Here is a Java while loop that prints the numbers 1 through 10. Java Loops   For, While, and Do


int loopvar = 1; // Declare and initialize the loop counter
while (loopvar <= 10) { // Test and loop

System.out.println(loopvar); // Print the variable
loopvar = loopvar + 1; // Increment the loop counter

The Java Do Loop

A Java do loop is similar to the Java while loop, except that the while test happens at the bottom of the loop. This means that the loop always executes at least once.

Java do Loop Syntax

Java do Loop Java Loops   For, While, and Do

The syntax of a do loop in Java is:


do {
statement(s)

} while (expression);

Example Java do Loop

Here is a Java do loop that prints the number 1 — even though the test fails.


int loopvar = 1; // Declare and initialize the loop counter
do {
System.out.println(loopvar); // Print the variable
loopvar = loopvar + 1; // Increment the loop counter
} while (loopvar >= 10); // Test and loop 

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Comments (1)

 

  1. chaman says:

    Thnks. Gud knwldge u hav. Helpful

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Java vs. JavaScript

    Java and JavaScript are two different programming languages which have similar names and share similar syntax. Java is a complete object oriented programming language which is designed to create applets or stand-alone applications. JavaScript is a more simple programming language which is designed to create scripts for use on the World Wide Web. Java bytecode [...]...


  • Java Operators

    Java Operator Operator Description [ ] Array index () Method call . Member access ++ Prefix or postfix increment -- Prefix or postfix decrement + - Unary plus, minus ~ Bitwise NOT ! Boolean (logical) NOT (type) Type cast new Object creation * / % Multiplication, division, remainder + - Addition, subtraction + String concatenation [...]...


  • Java vs. C++

    C++ supports pointers; Java does not (Java does have object references) C++ supports mutiple inheritance directly; Java supports multiple inheritance only through interfaces C++ supports operator overloading; Java does not C++ supports global variables; Java does not (sort of) C++ supports #define; Java does not C++ supports typedef; Java does not C++ supports enum; Java [...]...


  • Java Source Code

    Java source code is code that you write in the Java programming language. Java source code is converted to Java bytecode by the Java compiler. Java source code files usually have the .java extension. Sun recommends that Java source code files be no longer than two thousand lines. Larger source code files should be split [...]...


  • Java Compiler

    A Java compiler is a program which converts Java source code into Java bytecode. A basic Java compiler is included as part of the JDK (Java Development Kit). This Java compiler is called “javac”.              ...