JAVA TUTORIAL

Java Intro Java Features Java vs C++ Java Simple Program Java Applet vs Application JVM,JDK or JRE Java Keywords Java Variables Java Literals Java Separators Java Datatypes Java Operators Java Statements Java Array Java String

CONTROL STATEMENTS

Java If-else Java While Loop Java Do-While Loop Java For Loop Java For-each Loop Java Switch Java Break/Continue

JAVA METHODS

Java Method Java Overloading Java Overriding

JAVA CLASSES

Java OOPs Java Classes/Objects Java Inheritance Java Polymorphism Java Encapsulation Java Abstraction Java Modifiers Java Constructors Java Interface Java static keyword Java this keyword

Java do-while Loop.

In the last tutorial, we discussed while loop. In this tutorial we will discuss do-while loop in java. The Java programming language also provides a do-while statement

In java do-while loop is similar to while loop, the difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. That means the statements within the do block are always executed at least once.

Syntax:

do{
// code to be executed
}while(condition);

Working

First, the statements inside the loop execute and then the condition tested, if the condition returns true then the control gets transferred to the “do” else it jumps to the next statement after do-while.

Example:

// Java program to print number form 1 to 15

class Simple{
    public static void main(String args[]){

    int i = 1;

    do{
        System.out.println(i);
        i++;
    }while(i <= 15);
    }
}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Example: Java Infinitive do-while Loop

// Java program to print number form 1 to 10

class Simple{
    public static void main(String args[]){

    int i = 1;

    do{
        System.out.println(i);
    }while(i <= 15);
    }
}

Output:

1
1
1
1
1
1
1
.
.
.
ctrl+c

Note: ctrl+c is used for stop the execution of this program and exit form the program.