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 While Loop

The while statement continually executes a block of statements while a particular condition is true.

When condition is false, the control comes out of loop and jumps to the next statement after while loop.

Syntax:

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

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.



Note: The most important point to note when we using the while loop we need to use increment or decrement statement inside the while loop so that the loop variable gets changed on each iteration, and at some point condition returns false.


Example:

// Java program to print number form 1 to 10

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

    int i = 1;

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

Output:

1
2
3
4
5
6
7
8
9
10

Example: Java Infinitive While Loop

// Java program to print number form 1 to 10

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

    int i = 1;

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

Output:

1
1
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.