Write below programs & Execute it:

Answer :

Program 1 :
Output :

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

I am Batman

class WhileExample1{
    public static void main(String[] felight){
        int count=0;
        while(count<10){
            System.out.println("I am Batman");
            count++;
        }
    }
}

/*
D:\Felight\Basic Java>javac WhileExample1.java

D:\Felight\Basic Java>java WhileExample1
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
I am Batman
*/
Program 2 :
Output :

10

12

14

16

18

20

//Program to print even number from 10 to 20
class WhileExample2{
    public static void main(String[] felight){
        int from=10;
        int to=20;
        int loopControl=from;
        while(loopControl<=to){
            if(loopControl%2==0)
                System.out.println(loopControl);
            loopControl++;
        }
    }
}

/*
D:\Felight\Basic Java>javac WhileExample2.java

D:\Felight\Basic Java>java WhileExample2
10
12
14
16
18
20
*/

 

Program 3 :
Output :

10

9

8

7

6

5

4

3

2

1

 

//Program to print numbers from 10 to 1
class WhileExample3{
    public static void main(String[] felight) {
        int i=10;
        while(i>0){
            System.out.println(i);
            i--;
        }
    }
}

/*
D:\Felight\Basic Java>java WhileExample3
10
9
8
7
6
5
4
3
2
1
*/

 

Program 4 :

Program executes but while loop doesn’t.

Output : No output

 

//Zero loop iteration Example
class WhileExample4{
    public static void main(String[] felight) {
        int count=0;
        while(count>10){//condition is false
            //so loop will never execute
            System.out.println(count);
        }
    }
}

/*

D:\Felight\Basic Java>javac WhileExample4.java

D:\Felight\Basic Java>java WhileExample4

*/

 

Program 5 :

The below program will continue printing “Towards Infinity” for infinite times.

output : Infinite loop

The below program will give infinite number of outputs

//Infinite while loop Example
class WhileExample5{
    public static void main(String[] felight) {
        while(true){//Since codition itself is true
            //loop will never break
            System.out.println("Towards Infinity");
        }
    }
}
/*

D:\Felight\Basic Java>javac WhileExample5.java

D:\Felight\Basic Java>
WhileExample5.PNG
Program 6 :

output :

I am Batman 1

I am Batman 2

I am Batman 3

I am Batman 4

I am Batman 5

I am Batman 6

I am Batman 7

I am Batman 8

I am Batman 9

I am Batman 10

11

class TrickyWhile1{
    public static void main(String[] papan) {
        int count=0;
        while(count++ < 10)//Like "if" for while
            //also flower braces are optional
            //if its body contains only single
            //statement 
            System.out.println("I am Batman " + count);
        System.out.println(count);
        //above is equal to below
      /*
      while(count++ < 10){
         System.out.println("I am Batman " + count);
      }
      System.out.println(count)
      */
    }
}

/*
D:\Felight\Basic Java>javac TrickyWhile1.java

D:\Felight\Basic Java>java TrickyWhile1
I am Batman 1
I am Batman 2
I am Batman 3
I am Batman 4
I am Batman 5
I am Batman 6
I am Batman 7
I am Batman 8
I am Batman 9
I am Batman 10
11
*/

 

Program 7 :
output :

I am Batman 1

I am Batman 2

I am Batman 3

I am Batman 4

I am Batman 5

I am Batman 6

I am Batman 7

I am Batman 8

I am Batman 9

10

class TrickyWhile2{
    public static void main(String[] felight){
        int count=0;
        while(++count < 10){
            System.out.println("I am Batman " + count);
        }
        System.out.println(count);
    }
}

/*
I am Batman 1
I am Batman 2
I am Batman 3
I am Batman 4
I am Batman 5
I am Batman 6
I am Batman 7
I am Batman 8
I am Batman 9
10
*/

 

Program 8  :

Compiler Error.Because boolean type cannot be converted to int type.

class TrickyWhile3{
    public static void main(String[] felight) {
        int count=0;
        while(count=count++ < 10){ // incompatible types
            System.out.println("I am Batman " + count);
        }
        System.out.println(count);
    }
}

/*

D:\Felight\Basic Java>javac TrickyWhile3.java
TrickyWhile3.java:4: error: incompatible types: boolean cannot be converted to int
                while(count=count++ < 10){
                                    ^
TrickyWhile3.java:4: error: incompatible types: int cannot be converted to boolean
                while(count=count++ < 10){
                           ^
2 errors
*/

 

Program 9 :

Its an infinite empty while loop which will keep running for infinite times.

output : Infinite empty loop
class TrickyWhile4{
    public static void main(String[] felight) {
        int count=0;
        while(count < 10);//infinite empty loop
        {
            System.out.println("I am Batman " + count);
            count++;
        }
        System.out.println(count);
    }
}
/*

D:\Felight\Basic Java>javac TrickyWhile4.java

D:\Felight\Basic Java>java TrickyWhile4


 

Program 10 :

Its an empty while loop which will break when count becomes 10 and then will execute next block.

output :

I am Batman 11

 

class TrickyWhile5{
    public static void main(String[] felight){
        int count=0;
        while(count++ < 10);//empty loop{
            System.out.println("I am Batman " + count);
            count++;//count becomes 12
        }
    }
}
/*
D:\Felight\Basic Java>javac TrickyWhile5.java

D:\Felight\Basic Java>java TrickyWhile5
I am Batman 11
*/

 

Program 11 :
output :

6

class TrickyWhile6{
    public static void main(String[] papan) {
        int mysteryInt = 1;
        int count=1;
        while(mysteryInt < 3){
            mysteryInt=mysteryInt*count;
            count++;
        }
        System.out.println(mysteryInt);
    }
}

/*
D:\Felight\Basic Java>javac TrickyWhile6.java

D:\Felight\Basic Java>java TrickyWhile6
6
*/