Skip to main content

Conditions

Conditions are fundamental to programming. They allow us to preform certain actions based of the state of a program. A condition is defined as a comparison between two values using a comparison operator and enclosed in ().

Comparison Operators

These operators allow us to compare two values together. Similar to our arithmetic operators you need to think about what comparing two datatypes mean. You can't use create a condition that compares "hello" and 5 because is there is logical comparison between a String and an int. As stated above a condition in java is defined as comparison between two values using a comparison operator. The condition must be encompassed in (). The syntax for that would look like ([value] [comparison] [value]).

The are a few comparison operators each with their own behavior. Below is a table that shows the available comparison operators, a brief description of their function, and a code based example.

Operator Description Example
== equal
(1 == 2) // false
(false == false) // true
!= not equal
(1 != 2) // true
(false != false) // false
<, <= less than, less than or equal to
(-10 < 0) // true
(-10 <= -10) // true
>, >= greater than, greater than or equal to
(10 > 0) // true
(10 >= 10) // true

Logical Operators

What if we want to make a compound condition? Compare if one conditions is true and another false? Both true? Luckily our pal George Boole had us in mind when he crated the concept called boolean algebra. The fundamentals of boolean logic is three logical operator NOT, AND, and OR. Below are some "truth tables" which show the boolean output of comparing conditions using those logical operators.

NOT
Condition Output
false true
true false
AND
Condition A Condition B Output
false false false
false true false
true false false
true true true
OR
Condition A Condition B Output
false false false
false true true
true false true
true true true

Because our conditions return a boolean (true/false), these same logical operators can be used in code to compare conditions with one another. The tables below shows the logical operations available in Java. Remember that each true/false below can be replace with a condition.

NOT
!
!(false) // true
!(true) // false
AND
&&
(false && false) // true
(false && true) // false
(true && false) // false
(true && true) // true
OR
||
(false || false) // true
(false || true) // true
(true || false) // true
(true || true) // true

Condition Logic

If we want to run different segments of our cond based on the value of a condition we can use an if statement. The mighty if statement is a programmers favorite tool. It allows you to create powerful and unique behavior within your code.

If the condition in the parentheses is true, then the code in the block is run. Otherwise, Java skips over it.

Syntax
if(<condition>){
// Run This Code
}
Code Example
int number;
if((number / 2) == 0){
System.out.println("Number is Even");
}

You can add else statements on to if statements. The code in the else block is run if the condition is false

Syntax
if(<condition>){
// Run This Code
} else {
// Run Other Code
}
Code Example
int number;
if((number / 2) == 0){
System.out.println("Number is Even");
} else {
System.out.println("Number is Odd");
}

If we want to add multiple condition checks to a statement we can use an else if. They are checked if the previous statement is false. The code in the else block is run all the conditions are false.

Syntax
if(<condition>){
// Run This Code
} else if (<condition>) {
// Run Other Code
}
Code Example
int number;
if(number == 0){
System.out.println("Number is Zero");
} else if((number / 2) == 0){
System.out.println("Number is Even");
} else {
System.out.println("Number is Odd");
}

Ternary Operator

Programmers are lazy. Sometimes even too lazy to write an if-else statement for a simple condition, that preforms a very small action. Thats why they created the ternary operator ?. The ternary operator can be added to the end of a condition and allow for quick one line if-else statements. The syntax for using it is as follows. (condition)? <code if true> : <code if false>; A quick example show its power can be seen below.

// Traditional if-else statement
if (is_button_pressed){
doThisThing();
} else {
doThatThing();
}

// Ternary if-else statement
(is_button_pressed)? doThisThing() : doThatThing();

Switch Statement

Programmers are very lazy. If I have a bunch of values I want to compare to for a variable and preform specific code for each value it would be painful to write 15 if or if-else if statements. Thankfully we can use a switch statement. It will allow you to provide and single expression for comparison and then easily create a case for different each value it can take on. This is the basic syntax structure:

switch (<expression>) {
case <value>:
// Run code
break;
}

It can be confusing to understand just by reading so look at the example below.

int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Value not between 1 and 7");
break;
}
Console Output
Thursday
ALWAYS INCLUDE A break

If you forget to include a break at the end for your case it will cause the code to waterfall through following cases until a break or the end of the switch is reached. For example the code below would cause a waterfall from case 2 to case 4 because of the missing break.

int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
case 4:
System.out.println("Thursday");
break;
default:
System.out.println("Value not between 1 and 7");
break;
}
Console Output
Tuesday
Wednesday
Thursday
Combining Cases

Sometimes you can use the waterfall concept to your advantage if we have multiple values the expression can take, but want it to preform the same action.

switch (month){
case "December":
case "January":
case "February":
System.out.println("Winter");
break;
case "March":
case "April":
case "May":
System.out.println("Spring");
break;
case "June":
case "July":
case "August":
System.out.println("Summer");
break;
case "September":
case "October":
case "November":
System.out.println("Fall");
break;
default:
System.out.println("Not a real month!");
break;
}