The Coder's Handbook
Conditionals
BOOLEAN LOGIC
Boolean
A boolean value is either true or false. You can find a boolean in a few forms in Java:
A boolean expression, such as x > 5
A boolean variable, such as isAlive
A method that returns a boolean value such as isGameOver()
George Boole, for whom boolean is named after.
Fun Fact: If you look closely at his face, you can see a boolean expression.
Logical Operators
You can use logical operators to modify or combine boolean expressions. There are three logical operators in Java:
AND is represented with the && operator
These statements are true if both expressions are true and false otherwise
OR is represented with the || operator
These statements are true if the one or both expressions are true, and false if neither statement is true
NOT is represented with the ! operator
This simply reverses a single boolean expression
Comparison Operators
You can use comparison operators to evaluate two numerical values, resulting in a boolean value. There are six comparison operators in Java:
Greater Than >
Greater Than or Equal To >=
Less Than <
Less Than or Equal To <=
Is Equal To ==
Is Not Equal To !=
Tip #1 - Equality vs. Assignment
It is important to note when checking if two primitive types are equal, you must use the equality (==) operator rather than the assignment operator (=). If you use the assignment operator, often you'll find your logic is messed up even when the code compiles. That is A Very Bad Thing.
Code
// Asking "are you dead?"
isDead == true
// Literally killing a dude
isDead = true
Tip #2 - Chaining Comparisons
In mathematics, you can say something like 5 < x < 8. But this doesn't work in Java. Let's think about it. Imagine if x is 7. How would a computer evaluate this expression?
5 < 7 < 8
true < 8
*compile error*
Instead, we need to use two comparisons joined by a logical operator.
Does Not Compile
5 < x < 8
Proper Code
x > 5 && x < 8
IF / ELSE STATEMENTS
If Statements
An if statement branches your code's behavior to do something special when a boolean expression evaluates to true.
Syntax
if(boolean expression)
{
statement(s)
}
Eample #1
if(isHungry && hasMoney)
{
System.out.print("I am going to buy tacos");
}
Example #2
if(!isHungry || myMoney < tacoPrice)
{
System.out.print("I am not buying tacos today");
}
Tip #3 - Points For Style
Consider the examples below. You'll notice that while you can compare a boolean to true or false directly, it can make the code look a little clunkier than is needed. To make matters worse, you risk accidentally writing = instead of ==. As a result, saying == true or == false is considered bad style.
BAD STYLE
if(isHungry == true)
{
// code
}
GOOD STYLE
if(isHungry)
{
// code
}
BAD STYLE
if(isHungry == false)
{
// code
}
GOOD STYLE
if(!isHungry)
{
// code
}
Else
An if / else pair will branch your code one way if a boolean expression is true, and another way if it is false. Remember that an else statement must always come last, and you can only have one per if statement.
Syntax
if(boolean expression)
{
statement(s)
}
else
{
statement(s)
}
Example
if(number % 2 == 0)
{
System.out.print("The number is even");
}
else
{
System.out.print("The number is odd");
}
Else If
Finally, you can include any number of else if statements after an if statement. You can either have them on their own, or end the block with an else.
Syntax
if(boolean expression)
{
statement(s)
}
else if(boolean expression)
{
statement(s)
}
else
{
statement(s)
}
Example
if(dir == ‘N’)
{
System.out.println(“Go north!”);
}
else if(dir == ‘E’)
{
System.out.println(“Go east!”);
}
else if(dir == ‘S’)
{
System.out.println(“Go south!”);
}
else
{
System.out.println(“Go west!);
}
SWITCH STATEMENTS
Using Switch
When you have a lot of cases based on a single variable, you may find that a switch statement works better than an if statement. This doesn't offer any new functionality, but is very clean and organized. Consider the example code below, using an integer that stores an AP Score.
Code
switch(apScore)
{
case 5:
System.out.println(“You’re breathtaking!”);
break;
case 4:
System.out.println(“Good job, almost there.”);
break;
case 3:
System.out.println(“You’re passing, but not by much...”);
break;
case 2:
System.out.println(“Ruh roh.”);
break;
default:
System.out.println(“You have brought dishonor upon your family.”);
break;
}
RESOURCES
Alex Lee - Booleans
Alex Lee - Logical Operators
AND, OR, and NOT
Alex Lee - If Else Statements
Alex Lee - Comparison Operators
> >= < <= == !=