Heart containing Coding Chica Java 101

If only! Let’s Talk Conditions

TIP: References Quick List

Additional Info:

We can use if-then or if-then-else statements to control what portions of the code get executed on a given run. We provide a test or condition that describes whether or not to run each related block of code.

if (condition) {
   ...
}

if (condition) {
    ...this logic is run if the condition is true...
} else {
    ...this logic is run if the condition is false...
}

if (condition1) {
    ...this logic is run if condition1 is true...
} else if (condition2) {
    ...this logic is run if condition1 is false and condition2 is true
} else if (condition3) {
    ...else if blocks can be added however many times desired...
} else {
    ...if all conditions are false, then will run the else block...
}

While Java will let you have an if statement followed by a single instruction without the curly braces (not in a block), my recommendation is to always provide them explicitly. It is too easy for someone to accidentally change this code:

// Original code
if (condition)
    single statement that should execute if condition is true

To have an extra logging or other troubleshooting step added later on and actually introduce a new bug because of the lack of curly braces { and }.

// Later modification
if (condition)
    debugging log statement goes here
    single statement that should execute if condition is true

Java doesn’t care about indentation. Therefore, despite being indented, our single statement to execute only if the condition is true…now executes all the time. We could have avoided this bug just by adding the curly braces to the original code:

// Original code with explicit block added
if (condition) {
    single statement that should execute if condition is true
}

Booleans

In Java, there is a primitive called boolean that holds a value of either:

  • true, or
  • false

The conditions we use in if statements can either come from boolean fields/variables, or we can construct tests to determine the value on the fly.

Condition Test Operators

Java OperatorMeaningExample
==Equal to. This works well for primitives, but not objects.
More on that later.
score == 0
!=Not equal to. Again, works well for primitives, but not objects.score != 0
<Less than.score < 10
<=Less than or equal to.score <= 10
>Greater than.score > 20
>=Greater than or equal to.score >= 20
See also: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Conditional Operators

There are to operators we can use in Java to combine multiple tests into a single condition/statement.

Java OperatorMeaningExample
&&AND – both must be true.score > 90 && absences < 3
||OR – either or both could be true.score < 60 || absences > 10

Branches of Logic

If we have 2 tests that we combine together with either an AND or an OR condition, we have 4 branches of possible logic to consider with 2 tests when we start thinking about unit testing.

AND

When we use the AND operator, both sides must be true before the ANDed condition will be true. Note that we would have 4 branches of logic to then cover during unit testing.

Branch
Count
Test 1Test 2Condition Result
1truetruetrue
2truefalsefalse
3falsetruefalse
4falsefalsefalse

OR

When we use the OR operator, either or both sides being true will cause the ORed condition to be true. Similarly here, we would again have 4 branches of logic to cover during unit testing.

Branch
Count
Test 1Test 2Condition Result
1truetruetrue
2truefalsetrue
3falsetruetrue
4falsefalsefalse

Type Comparisons

Since we haven’t gotten into objects in Java much yet, let’s skip this type of test for now.

If only! Let’s Talk Conditions

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.