D. Ace
D. Ace

Reputation: 418

Understanding an If statement in Java

I am having problems understanding the following code:

public class TestIf {


    public static void main(String[] args) {
        if (true){
            if (false){
                System.out.println("true false");
            }

            else{
                System.out.println("true true");
            }

        }

    }

}

When I run this it prints true true.

I don't understand why this bit of code gets executed in the first place:

if(true)

If what exactly is true here? It's not like a have declared a boolean, for example

boolean bol = true;

if (bol == true) { 
 //execute the rest of the code
}

Upvotes: 1

Views: 136

Answers (5)

HelloWorld123456789
HelloWorld123456789

Reputation: 5359

If the expression inside if gets evaluated to true, the if is entered.

boolean bol = true;

if (bol == true) { 

What happens above? Is bol equal to true? Yes. So (bol==true) is equivalent to writing only (true).

So the above code is same as

if (true) {

So now consider your code.

    if (true){ // enters 'if' since value of expression inside 'if' is true
        if (false){ // goes to else
            System.out.println("true false");
        }

        else{
            System.out.println("true true"); // prints
        }

    }

Upvotes: 2

CodeCamper
CodeCamper

Reputation: 6980

if(true)

Is the same thing as saying no matter what this if statement will always return true. The only time you want to do something like this in an if statement is for quick and dirty testing purposes.

if(false)

Is the same thing as saying no matter what this if evaluation will always fail. Same as before. You would never do this in real life in an if statement. Your example of the Boolean is the same exact thing. But in this case you are skipping that extra line of code to declare the Boolean with a name. The only reason you declare a variable with a name is so you can reuse it later or change it. In this case you are simple placing a Boolean inside the if which will be lost forever as soon as you get out of the if statement.

Sometimes however this true/false evaluation can be useful. For example

Boolean test = true;
while(true)
{
//ask for user input...
if(test) break;
}

The while will always evaluate true and will forever loop UNTIL it receives a break command. However I don't know any cool tricks with the if statement this way other than running methods from inside the if.

Upvotes: 2

Alec Henninger
Alec Henninger

Reputation: 360

The syntax of an if statement is so:

if (expression) {
}

Inside the () you put an expression that can evaluate to true or false. If the expression evaluates to true, the if statement will execute the block of code underneath the if. Otherwise, your interpreter will move on to the else statement, where this can execute another if statement, or just default to some block of code.

In your example, the expression is simply true, and therefore always evaluates to true, so that if block always executes.

The same logic applies to false: false of course always evaluates to false, and so those blocks are never executed.

Upvotes: 0

herohuyongtao
herohuyongtao

Reputation: 50667

For an if statement like:

if (condition)
{
    statements1;
}
else
{
    statements2;
}

It will first evaluate the condition, if condition==true, statements1 will be evaluated, otherwise, statements2 will be evaluated.


Back to your example:

if (true){
    if (false){
        System.out.println("true false");
    }
    else{
        System.out.println("true true");
    }
}

is equivalent to

if (false){
    System.out.println("true false");
}
else{
    System.out.println("true true");
}

and is further equivalent to

System.out.println("true true");

Upvotes: 0

jhamon
jhamon

Reputation: 3691

In Java , true and false are constants.

So a block starting with

if(true)

will always be executed whereas a block starting with

if(false)

will never be executed

Doing something like

boolean bol = true;

... // more process here

if (bol) { 
 //execute the rest of the code
}

is different as the bol varaible can either be true or false, depending on the previous processing

Upvotes: 0

Related Questions