While Loop In Java -10 Programs
“The best is yet to be”
WHILE LOOP In Java
While Loop : Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement
Syntax of while loop
while(condition)
{
statement(s);
}
Diagram:
How while Loop works?
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, the control comes out of loop and jumps to the next statement after while loop.
Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false.
Problem 1://Printing 1 to 10 numbers using while loop
class WhileExample1 {
public static void main(String args[]){
int i=1;
while(i<10){ //upto the 9
System.out.println(i);
i++;
}
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
Problem 2:
class SimpleExample {
public static void main(String args[])
{
// initialization expression
int i = 1;
// test expression
while (i < 6) {
System.out.println(“Hello World”);//it will execute 5 times from 1to 5
// update expression
i++;
}
}
}
OUTPUT:
Hello World
Hello World
Hello World
Hello World
Hello World
Problem 3://simple While loop program to print 10 to 1 numbers.in reverse fashion.
Code:
class While_Example2 {
public static void main(String args[]){
int i=10;
while(i>= 1){
System.out.println(i);
i — ;
}
}
}
Output:
10
9
8
7
6
5
4
3
2
1
Note*:Here we are printing reverse order so I had decreament pointer.
Problem 4: This program will find the summation of numbers from 1 to 10.
Code:
// Java program to illustrate while loop
class SumUsingWhile_Loop {
public static void main(String args[])
{
int x = 1, sum = 0;
// Exit when x becomes greater than 4
while (x <= 10) {
// summing up x
sum = sum + x;
// Increment the value of x for
// next iteration
x++;
}
System.out.println(“Summation: “ + sum);
}
}
OutPut:
Summation: 55
Problem 5://Prints 5th table using while loop
Same procedure like in for loop we did.only sytaxes change
CODE:
class BreakExample2 {
public static void main(String args[]){
int n=5;
int i=1;
while(i<=10){
System.out.println(n*i);
i++;
}
}
}
OUTPUT:
5
10
15
20
25
30
35
40
45
50
PROGRAM 6://Factorial of Given Number
Here number we have taken is 5 .so factorial of 5 is 120.
5!=5*4*3*2*1=120
CODE:
public class JavaExample {
public static void main(String[] args) {
//We will find the factorial of this number
int number = 5;
long fact = 1;
int i = 1;
while(i<=number)
{
fact = fact * i; //storing all multipilcation in fact
i++;
}
System.out.println(“Factorial of “+number+” is: “+fact);
}
}
OUTPUT:
Factorial of 5 is: 120
PROGRAM 7: Printing character in a string using while loop
CODE:
class WhileInfinitive_Loop {
public static void main(String args[]){
String str=”sachinTendulkar”;
//i starts with 0 as array index starts with 0 too
System.out.println(“Printing character using while loop :”);
int i=0;
while(i<str.length()){
System.out.println(str.charAt(i));
i++;
}
}
}
OUTPUT:
Printint character using while loop :
s
a
c
h
i
n
T
e
n
d
u
l
k
a
r
PROGRAM 8: //Printing string in reverse order character by character using for loop
CODE:
//Printing string in reverse order character by character
class JavaExample {
public static void main(String args[]){
String str=”sachinTendulkar”;
//i starts with 0 as array index starts with 0 too
System.out.println(“Printing String in reverse fashion:”);
int n=str.length();
while(n>0){
System.out.println(str.charAt(n-1)); //it will start at n-1 position //and then decreament upto 0
n — ;
}
}
}
OUTPUT:
Printing String in reverse fashion:
r
a
k
l
u
d
n
e
T
n
i
h
c
a
s
PROGRAM 9: Printing Infinite times in infinite many time using while loop
CODE:
//Printing Infinite times in infinite many time using while loop
class WhileInfinitive_Loop {
public static void main(String args[]){
int i=10;
String str=”Infinite times”;
while(i>1)
{
System.out.println(str);
i++;
}
}
}
OUTPUT:
Infinite times
Infinite times
Infinite times
Infinite times
Infinite times
Infinite times
Infinite times
Infinite times
And so on…….
PROGRAM 10://Printing Array elements suing while loop
Note*:U don’t know about array then don’t worry .Here array simple format will be arr[]={2,11,45,9} like this .So basically array store same datatype multiple value.
CODE:
class WhileLoop_Array {
public static void main(String args[]){
int arr[]={2,11,45,9};
//i starts with 0 as array index starts with 0 too
int i=0;
while(i<4){
System.out.println(arr[i]);
i++;
}
}
}
OUTPUT:
DO WHILE Loop
If u want understand do while loop then contact me.
Thank you;