Interpret the program and write the Output, then write the program and execute to verify it. If your answer was wrong then understand why it is and write a brief note about how program is working.


Answer :

Program :  1

In the below the conditional statement program the condition of  if is true so it will execute its single statement if block. And the output would be :

This is if block

I am Batman

class If1{
    public static void main(String[] felight){
        if(true) // condition is true
            System.out.println("This is if block"); // this single statement of if will execute
        System.out.println("I am Batman"); //this statement will execute as its outside of if block 
    }
}
/*
D:\Felight\Basic Java>javac If1.java

D:\Felight\Basic Java>java If1
This is if block
I am Batman
*/

 

Program :  2

In the below program if block will not execute as the condition was false.So the output would be :

I am Batman

class If2{
    public static void main(String[] felight){
        if(false)//condition is false
            System.out.println("This is if block");
        //the above statement will not execute
        System.out.println("I am Batman");
        //the above statement will execute
    }
}
/*
D:\Felight\Basic Java>javac If2.java

D:\Felight\Basic Java>java If2
I am Batman
*/
Program :  3

In the below program the multi-statement if block will not execute as the condition is false.So the output would be nothing.

class If3{
    public static void main(String[] felight){
        if(false){ //condition false
            System.out.println("This is if block");
            System.out.println("I am Batman");
            //both the above statement will not execute as 
            //both are inside if block
        }
    }
}
/*

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

D:\Felight\Basic Java>java If3

*/

 

Program :  4

In the below program the if statement will not execute because the condition is false.So the output will be :

I am Batman

class If4{
    public static void main(String[] felight){
        int x=1000;
        if(x==100)
            //condition is false 
            System.out.println("This is if block");
        // above statement will not execute
        System.out.println("I am Batman");
        //above statement will execute
    }
}
/*

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

D:\Felight\Basic Java>java If4
I am Batman

*/
Program :  5

In the below program the if statement will be execute because condition is true. So the output will be :

This is if block

I am Batman

class If5{
    public static void main(String[] felight){
        int x=1000;
        if(x==1000)
            //condition true
            System.out.println("This is if block");
        //Above statement will execute
        System.out.println("I am Batman");
        //this will also execute
    }
}
/*

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

D:\Felight\Basic Java>java If5
This is if block
I am Batman
*/

 

Program :  6

In the below program the if condition is true but it will still not compile because we cannot declare a variable in a single if statement.It pointless declaring a variable if we cant access it from inside or outside of if block.So its a compile error.

class If6 {
    public static void main(String[] felight){
        int x=1000;
        if(x==1000)
            //condition true
            int y=600;
        //but the compiler will give an error
        //because we cant declare variable in a
        //single if statement because
        //it will not be visible outside of if block
        System.out.println(y);
    }
}
/*
D:\Felight\Basic Java>javac If6.java
If6.java:6: error: variable declaration not allowed here
                        int y=600;
                            ^
1 error
*/

 

Program :  7

In the below program the multi-statement if block will execute as the condition is true.So the output would be:

1000

1001

class If7{
    public static void main(String[] felight){
        int x=1000;
        if(x==1000){
            //condition is true 
            int y=600;
            //this time above statement is valid 
            //because its a multi-statement if block
            //and we can access variable y
            //from inside the if block
            System.out.println(x++);
            //the above statement will print x=1000
            //and then will increment x by 1
        }
        System.out.println(x++);
        //this statement will print x=1001
        //and then will increment x by 1
        //now variable x holding integer value 1002
    }
}
/*
D:\Felight\Basic Java>javac If7.java

D:\Felight\Basic Java>java If7
1000
1001
*/