## 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 > b`is 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
}
``````

## Java Tutorial #7 — Nested If / else structure and logic

*Note*: My apologies for the volume being somewhat screwed up, Starcraft II turned up microphone boost on its own. This gets fixed in a few videos.

Topics Discussed: The structure behind using nested if statements effectively and how easy it is to get them confused!

Source Code Available Here

Nested if structure
In this video I give you a fairly simple example as to why we should avoid nested if statements whenever we possibly can. The primary reason behind this is because they syntax is overwhelming, and it simply becomes very difficult to read / understand.

The syntax of a nested if is exactly the same as a normal if / else statement, except that it takes place in the ‘payload’ of an existing if / else statement.

There isn’t really too much to write up about this lesson, as it built off of what we covered in the lesson before now. Just watch your syntax and you should be able to grasp this almost immediately.

## Java Tutorial #8-1 / 8-2 — Using math functions with if logic

Apologies about this being a two-part video, AND for my mic volume being weird, in future videos this is fixed. I’ll consider re-recording this later

Topics Discussed: Using math functions with if-based logic to choose the output of a program

Source Code Available Here

8-2 Here

Topics discussed: Same as above

Source Code Available Here

In these videos I talk a little bit about java.lang.Math; An include that allows us to do some simple mathematical calculations in our code in conjunction with the if-statements we’ve been talking about.

java.lang.Math functions
There are a LOT of different constructs in the java.lang.Math library, but the ones we use in this lesson seem to be the most common of them. Raising a number to a power, and taking the square root of a number. The code for each of these is as follows: (assuming you have the import in your code)

``````
Math.pow(numberOrVariable, powerRaisingTo);
Example:
Math.pow(x, 3); // raises x to the 3rd power.

Math.sqrt(x); //Takes the square root of x.
``````

## Java BugHunt #5: If Statements

Difficulty: Easy
Time to solve: <5 min
Video this is covered in: 6

``````

import java.util.Scanner;
public class Jtutorial1 {
public static void main(String args[]){
Scanner input = new Scanner(System.in);

System.out.println("How much does Employee 1 make? ");
double emp1 = input.nextDouble();

System.out.println("How much does Employee 2 make? ");
double emp2 = input.nextDouble();

System.out.println("How much does Employee 3 make? ");
double emp3 = input.nextDouble();

if (emp1 > emp2){
if (emp1 > emp3){
System.out.println("Employee 1 makes more than the other 2 employees");
}
else{
System.out.println("Employee 3 makes more than Employee 1.");
}
else{
System.out.println("Employee 2 makes more than employee 1");
}

}//end main
}//end class

``````

Input:
500
400
300

Program Output:
How much does Employee 1 make?
How much does Employee 2 make?
How much does Employee 3 make?
Employee 1 makes more than the other 2 employees
Error information:Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – ‘else’ without ‘if’
at Jtutorial1.main(Jtutorial1.java:22)