Conditionals
Last updated
Last updated
Iowa City Robotics 2024
This lesson will focus on conditionals. We will begin by looking at what conditionals are and the different types of conditional logic and statements.
Conditionals are code statements that allow a program to choose what code to run when encountering different situations. For example, a videogame may need to choose between opening a door when the player has a key and telling the player that they donβt have a key.
In Java, all conditional logic outputs booleans, and most conditional statements take booleans as inputs. Java has a variety of built-in conditional logic, mainly derived from mathematics:
Conditional Logic | Name | Description |
---|---|---|
Conditional logic can also be methods that return boolean values.
<
,>
,<=
,>=
,==
, and!=
can only be used on number data types such as ints and doubles. They cannot be used to compare Strings. For comparing Strings, use.equals()
. See Object Oriented Basics for more info.Likewise,
||
,&&
,^
, and!
can only be used on boolean values.
Java has a number of conditional statements that will choose between different code based on the input.
Syntax:
If statements, as their name implies, run code if the conditional is true
. Optionally, it can run else
statements if the conditional is false
.
You can chain if/else statements together by using else if statements, however, it is not recommended as itβs harder to read and usually worse-performing than switch statements.
Java does not have a statement that combines
else
andif
, such as Pythonβselif
.
Syntax:
A while loop continuously repeats code inside of it while the statement is true, checking once before running the code.
Another method of using while loops, although not recommended, is using a while true loop with a break statement. While true loops will keep running code inside it forever until the code inside calls a break statement, so if the break statement fails to run properly for whatever reason, it will stall the program.
Do while
Do while loops are, effectively, just normal while loops with different syntax.
Syntax:
Switch statements are a special type of conditional statement, not requiring a boolean input. Instead, it can take any data type as its input. It will look from the top to the bottom, checking to see if the input matches any of the cases, and if not, it will look for a default case, running the code inside the case that matched first.
The following if/else chain is functionally equivalent to a switch statement:
De Morgan's laws state that boolean statements can be written in multiple ways and still have the same effect. To make a long story short, it's similar to multiplicative distribution in math.
For example, x*y + z*y
is the exact same as y*(x+z)
. Likewise, !a && !b
and !(a && b)
both have the exact same output.
Some other examples would be:
One of Java's little quirks is that it will skip evaluating the rest of a condition if it can assume the output. For example, it will not fully evaluate false && true
as Java sees false &&
and automatically assumes the statement to be false.
This can be avoided if you use &
or |
instead of &&
or ||
as they force Java to not short-circut.
Example code:
Example code: (No short-circut)
Statement | Functional equivalent |
---|---|
A < B
Less than
Outputs true
if the value of A
is less than B
.
A > B
Greater than
Outputs true
if the value of A
is greater than B
.
A <= B
Less than or equal to
Outputs true
if the value of A
is less than or equal to B
.
A >= B
Greater than or equal to
Outputs true
if the value of A
is greater than or equal to B
.
A == B
Equal to
Outputs true
if A
is EXACTLY equal to B
.
A != B
Not equal to
Outputs true
if A
ISNβT exactly equal to B
.
A || B
Or
Outputs true
if EITHER the value of A
or B
is true
.
A && B
And
outputs true
if BOTH the value of A
and B
is true
.
A ^ B
Xor/Exclusive or
Outputs true
if either the value of A
or B
is true
but outputs false
if BOTH are true
.
!A
Not
Outputs whatever is the inverse of A
, such as false
if A
is true
, or true
if A
is false
.
a && b
!(a || b)
(a && b) && (a && c)
a && (b && c)