Timothy Kardaras
Timothy Kardaras

Reputation: 123

Nested If Statement

I have a code written below which is pretty much one if statement nested into another, therefore when running the code below if the first if statement is incorrect the nested if statement won't run either.

I know this means that you aren't able to make the 2nd if statement true but my question is simple:

Is the 2nd if statement true but ignored due to the 1st if statement being false or is the 2nd if statement false altogether?

The code for my question is below:

int temperature;
myWindow.clearOut();
temperature = Integer.parseInt(myWindow.readIn());
if (temperature >= 18) {
    myWindow.writeOutLine(temperature + " is greater than or equal to 18");
    if (temperature % 2 == 0)
        myWindow.writeOutLine(temperature + " is an even number");

}

Upvotes: 0

Views: 1082

Answers (5)

Arun George
Arun George

Reputation: 1209

The 2nd if statement if (temperature % 2 == 0) may or may not be true, but is executed only when the 1st if statement if (temperature >= 18) is true.

Upvotes: 0

The second if condition is actually conditioned to the first one, because you will never know if the temperature is even until it is true that it is greater than 18...

such a cascade conditions can be a pit fall to find bugs in the application...

I will suggest to reorder the conditions

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

There are three alternative ways to do this - you must decide for yourself.

/**
 * Warning on temp >= 18.
 * Warning on temp >= 18 && temp is even.
 */
public void test1() {
    if (temperature >= 18) {
        myWindow.writeOutLine(temperature + " is greater than or equal     to 18");
        if (temperature % 2 == 0) {
            myWindow.writeOutLine(temperature + " is an even number");
        }
    }
}

/**
 * Warning on temp >= 18.
 * Warning on temp is even.
 */
public void test2() {
    if (temperature >= 18) {
        myWindow.writeOutLine(temperature + " is greater than or equal     to 18");
    }
    if (temperature % 2 == 0) {
        myWindow.writeOutLine(temperature + " is an even number");
    }

}

/**
 * Warning on temp >= 18.
 * Warning on temp < 18 && temp is even.
 */
public void test3() {
    if (temperature >= 18) {
        myWindow.writeOutLine(temperature + " is greater than or equal     to 18");
    } else if (temperature % 2 == 0) {
        myWindow.writeOutLine(temperature + " is an even number");
    }
}

Upvotes: 0

Andy
Andy

Reputation: 1994

You don't know whether the 2nd if-statement would evaluate to true or false because it gets not executed if the 1st if-statement evaluates to false.

That is how modern programming languages work - the code that should be executed is added to a stack of commands. If the 1st statement evaluates to false, nothing within the if's body is added to the stack.

Upvotes: 2

Timothy Kardaras
Timothy Kardaras

Reputation: 123

the nested if statement is neither true or false as it is never evaluated and ignored completely. (Thanks to Ori and Berger).

Upvotes: 0

Related Questions