Java Tutorial #6 — Intro to if/else logic


Topics Discussed: If/Else logic and structure, if “conditionals”

Source Code Available Here

If statements
An if statement has two essential elements, the condition under which it will run (the conditional) and the payload that it will run when the condition is true. Let’s examine each part in a bit more depth:

If conditional: The if conditional is the part of the if statement where we tell the if under which conditions it will be operating. Here’s an example of an if conditional:


     if ( a ==7 ){
          //do stuff here
     }

In this case the contents of the brackets will be executed under the condition that the variable a is equal to 7. In the event that it’s not, nothing will happen here, however if you added the code:

     else{
          //do stuff
     }

You would have the option to add something to happen when a did NOT equal 9.


Boolean conditionals
The basic idea behind a statement evaluating to either ‘true’ or ‘false’ like the above conditions is known as ‘boolean’ logic. With that being said, there are things called ‘boolean operators’ that allow us to compare our variables to other constructs. The main boolean operators we will be using throughout these videos are as follows:


<     // Less Than
>     // Greater than
<=    // Less than or equal to
>=    // Greater than or equal to
!     // Not or is not
!=    // Is not equal to (very similar to above)
==    // is equal to
||    // Or
&&    // And
true  // will evaluate to true
false // will evaluate to false

Using the above statements we can make complex comparisons with relative ease, like this:

if (a < =6 && a > b || a < c && a > x){
     //do stuff
}
else{
     //do other stuff
}

In the next lesson we talk about boolean variables which always evaluate to either true or false. But what is true or false, and how to if statements actually resolve themselves?


If resolution
One of the more misunderstood areas of if statements is how they work internally, and the basic idea is something like this. If a statement evaluates to 0, then it is false, if a statement evaluates to 1, it is true. Let me show you an example where you can overlook this, then one where it’s in your face.


int a = 1;
if ( a ==1 ){ //this evaluates to 1, or true
     //do stuff
}

In this case, the contents being if (1) doesn’t really matter all that much. But in the following example, it might make sense why I’m teaching this at all:

int a =6, b=7, c=8;

if (c> b > a){ if 6 < 7< 8
     System.out.println("Expected output");
}
else{
     System.out.println("Unexpected output");
}

In this case the ‘unexpected output’ would appear on our screen, but why? Remember how a few lessons ago I mentioned that arithmetic evaluates from left – to – right in cases where items have the same precedence? That’s exactly what’s happening here. We are comparing c(8) to b(7) and seeing if c is > b. In this case c > bis true, so c > b becomes “1” (true). The remaining statement is 1 > a(where a is equal to 6), therefore the entire thing evaluates to 0, because 1 is less than 6. The proper way to write the above statement would be something like this:

if (c > b && c >a){
   //do stuff
}

Leave a Reply

Your email address will not be published. Required fields are marked *