Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

While loop in Java programming with example

Posted on December 6, 2016December 26, 2019

Table of Contents

  • While loop in Java programming with example
  • Loops in Java
    • Syntax of while loop
    • While loop flow diagram
  • Infinite while loop

While loop in Java programming with example

In this article, we will discuss  the While loop in Java programming language.

In this post, we are going to learn how to use while loop in Java programming language

Loops in Java

loops are used to execute a block of code as long as a specified condition is satisfied.

Java program has three types of basic loops

  • for loop
  • while loop
  • Do-while loop
While loop
while  is used to execute a statement(s) repeatedly until the particular condition is satisfied, in the tutorial, we will learn how to work while loop with examples
First, we will know while loop syntax.

Syntax of while loop

while(condition(s))
{
Body of loop
//statement(s);

}

In while loop, the test condition is evaluated first and if it returns true then executes inside the body of the statement. if condition returns false, The flow of control goes out of loop body.

"While

While loop flow diagram

  • The loop  begins with initialization part it is executed only once
  • Then check the test expression, boolean expression may return true/false .When boolean expression is true, codes inside the body of while loop is executed.
  • Then control goes to increment part or decrement part and update statement is updtaed
  • Test expression of while loop is evaluated on each iteration until the boolean expression returns false
  • When the test expression is returned to false, while loop is terminated.
While loop example
Example 1
class Whiledemo{
public static void main(String args[]){
int x;
x=1;
while(x<=10)
{
System.out.println("x is " + x);
x++;
}
 
}
}

When the above code is executed, it produces the following result

 
Example 2
import java.util.Scanner;
class Sum_Of_Num{
public static void main(String args[]){
  int sum=0;
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the number as you wish: ");
int num=scan.nextInt();//get input from the user for num
int i=1;
while(i<=num){
  sum+=i;  //sum=sum+i;
  i++;
}
System.out.println("Sum of 1 to "+num+" is: "+sum);

}


}

When the above code is executed, it produces the following result

Enter the number as you wish:100
sum of 1 to 100 is:5050

 

Example 3

import java.util.Scanner;
class While_Display{
public static void main(String args[]){
  int sum=0;
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the number as you wish: ");
int num=scan.nextInt();//get input from the user for num
int i=1;
while(i<=num){
System.out.println("This while loop displays "+i+" times");
i++;
}

}


}

When the above code is executed, it produces the following result

Enter the number as you wish: 12
This while loop displays 1 time
This while loop displays 2 time
This while loop displays 3 time
This while loop displays 4 time
This while loop displays 5 time
This while loop displays 6 time
This while loop displays 7 time
This while loop displays 8 time
This while loop displays 9 time
This while loop displays 10 time
This while loop displays 11 time
This while loop displays 12 time

Infinite while loop

 

Output

1
2
3
4
5
6
7
8
……..
………
never end;


Above is the infinite while loop in Java. Test condition is never satisfied. So program flow never ends.

Related Articles

while Loop in C++While Loop In C

Do-while Loop in JavaDo-while Loop C++ Do-while loop in C

For Loop in C++For Loop in CFor loop in java

If condition C++If condition in JavaIf condition in C

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes