-3
class ee
{
    public static void main(String args[])
    {
        boolean a;
        a=true;
        System.out.println("Answer is "+a);
        a=false;
        System.out.println("Answer is "+a);
        if(a) System.out.println("This is not executed");
        a=true;
        if(a)
        System.out.println("This is executed");
        System.out.println("10>11 is "+(10>11));
    }
}

Output:

Ansewr is true Answer is false This is executed 10>11 is false

Why it is showing "This is executed" in the third line of the output? Why "This is not executed" is not there in the output ?

Yash
  • 41
  • 1
  • 6
  • 1
    You have to read some documentation about [`if` statement](http://www.tutorialspoint.com/java/if_else_statement_in_java.htm) – tfosra Jun 15 '16 at 10:10
  • I think you should use brackets with your `if`s to avoid errors in the corresponding block of statements. – Kit Ostrihon Jun 16 '16 at 11:09

5 Answers5

1

Let's look each line of the code.

class ee
{
    public static void main(String args[])
    {
        boolean a;

        a=true;
        System.out.println("Answer is " + a);  // OUTPUT Answer is true

        a=false;
        System.out.println("Answer is " + a);  // OUTPUT Answer is false

        //You are basically saying that if the statement is true, do this part(Inside the curly brackets)
        // a = false here so the program will not do inside the curly brackets
        if(a)
        {
            System.out.println("This is not executed");
        }

        a=true;

        // a = true here so the program will do inside the curly brackets
        if(a)
        {
            System.out.println("This is executed");
        }

        System.out.println("10>11 is "+(10>11));
    }
}

I guess you know about if statements, but If you want to learn more about using if statements go here

Ahmet Batu Orhan
  • 118
  • 2
  • 14
0

Because a is false in the first if, and true in the second. The statement inside the if sentence is executed only if the if's condition is true

SCouto
  • 7,808
  • 5
  • 32
  • 49
0
         a=false;
        System.out.println("Answer is "+a);
        if(a) System.out.println("This is not executed");
        a=true;
        if(a)
        System.out.println("This is executed");

If you look at your code, you are setting a to false, if(a) by default assumes a is true, so it does not show "This is not executed". You are again setting a to true, so here it now shows "This is executed".

0

I hope it would help you, go through each comments properly,

boolean a;  //default value false
                a=true; //assinged 'a' to true
                System.out.println("Answer is "+a); //so it prints true here
                a=false;   //again assigned it to false
                System.out.println("Answer is "+a);  // so it prints as false here
                if(a) System.out.println("This is not executed");
        /*
        the above line means as if(a){// a is false here, then it wont go inside, HENCE IT IS NOT THERE IN OUTPUT
        System.out.println("This is not executed");
        }

        */
                a=true; // again a assigned as true
                if(a)
                System.out.println("This is executed");
    /*
        the above line means as if(a){// a is true here, then it goes inside, HENCE IT IS THERE IN OUTPUT
        System.out.println("This is executed");
        }

        */
san544
  • 72
  • 10
0
    a=false;
    System.out.println("Answer is "+a);
    if(a) System.out.println("This is not executed");
    a=true;
    if(a)
    System.out.println("This is executed");
    System.out.println("10>11 is "+(10>11));

The a = false changes a to false and hence if(a) System.out.println("This is not executed"); is not executed then passing a=true; makes a true and thats why System.out.println("This is executed"); bgot executed

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65