TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java while loop

Java while loop with java nested while loop, java while loop examples, java infinitive while loop example, difference between java for loop and while loop with concepts and examples.

<< Back to JAVA

Java While Loop

The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.

The while loop is considered as a repeating if statement. If the number of iteration is not fixed, it is recommended to use the while loop.

Syntax:

while (condition){  
//code to be executed 
I ncrement / decrement statement
}  

The different parts of do-while loop:

1. Condition: It is an expression which is tested. If the condition is true, the loop body is executed and control goes to update expression. When the condition becomes false, we exit the while loop.

Example:

i <=100

2. Update expression: Every time the loop body is executed, this expression increments or decrements loop variable.

Example:

i++;

Flowchart of Java While Loop

Here, the important thing about while loop is that, sometimes it may not even execute. If the condition to be tested results into false, the loop body is skipped and first statement after the while loop will be executed.

flowchart of java while loop

Example:

In the below example, we print integer values from 1 to 10. Unlike the for loop, we separately need to initialize and increment the variable used in the condition (here, i). Otherwise, the loop will execute infinitely.

WhileExample.java

public class WhileExample {
public static void main(String[] args) {
    int i=1;
    while(i<=10){
    	System.out.println(i);
    i++;
    }
}
}
Test it Now

Output:

1
2
3
4
5
6
7
8
9
10

Java Infinitive While Loop

If you pass true in the while loop, it will be infinitive while loop.

Syntax:

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

Example:

WhileExample2.java

public class WhileExample2 {  
public static void main(String[] args) { 
 // setting the infinite while loop by passing true to the condition
    while(true){  
        System.out.println("infinitive while loop");  
    }  
}  
}  

Output:

infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

In the above code, we need to enter Ctrl + C command to terminate the infinite loop.


Next TopicJava do-while Loop




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf