Conditional statements are used to perform different actions based on conditions that you specify.
Let’s look at the different components of a conditional statement.
There are several operators you can use to create a condition.
The result of this operation will output a boolean, true or false.
A condition is usually comprised of two data values and an operator,
sometimes wrapped in parenthesis.
They are most commonly found on if else statements and loops.
Here’s some common comparison and equality operators you can use in your expressions.
Less than, less than or equal to, greater than, greater than or equal to, equal, and not equal.
There are also a few logical operators you can use to chain statements
to provide more specific conditions.
The most common of these is the and operator, double ampersand,
which is placed between two conditions.
It checks the condition before and the condition after, and if both are true,
the full condition is met and will output true.
Another good one is the or operator, double pipe symbol, that is also placed between two conditions.
If either condition is true, the statement outputs true.
There’s also a not operator, exclamation mark, that is placed before a condition.
It basically inverts the condition that follows.
If the condition is true, it causes the statement to output false, and vice versa.
You can combine these statements using a series of parentheses to chain them together
for more complex structures.
An if statement is a conditional statement to check if a condition is true or false.
It uses an operation to determine if the values are equal to or not equal to each other.
Based on the outcome of the if operation, the statement will evaluate the
if section's statements or skip it entirely.
Here's a basic if statement.
The condition is evaluated to see if a is greater than b.
If it is, the statement inside the curly brackets will be evaluated.
If the condition is false, the curly brackets are skipped and the expression moves to the next line.
An if...else statement is identical to an if statement, just with an added else clause
to evaluate when the if condition is false.
This example checks to see if a is greater than b.
If so, the statement’s curly brackets are evaluated and the else curly brackets are skipped.
If the operation is false, the if statement’s curly brackets are skipped
and the else curly brackets are evaluated instead.
An if...else if...else statement looks and sounds pretty complex,
but it just adds a secondary else if condition in between the first if condition and the last else clause from before.
When the if condition fails, the else if condition is evaluated, and if it results in true,
the curly brackets are evaluated and the final else clause is skipped.
You can include as many else if statements as you like between the if and the else conditions.
The ternary operator, or inline if statement, is a compact and efficient form of the conditional statement.
When working with simple values, it’s a great way to clean up code.
The condition is evaluated as a boolean, true or false, and then evaluates the
corresponding statement based on the outcome.
So, if the condition is true, the first value or statement after the question mark is used.
Otherwise, the second value after the colon is used instead.
You can use this to rewrite the earlier condition statement like this.
These two examples have identical output, only their formatting is different.
If you’re working with complex statements or trying to nest statements inside of each other,
the ternary operator becomes hard to edit and understand.
But for simple calculations and values, it can be the preferred, cleaner alternative
to the if statement.
We've arrived at the end of Lesson 505:
Formatting Conditional Statements to Control an Expression Output
If you enjoy this expressions course, consider purchasing the paid content.
It includes in-depth documentation, extra tutorial content, high definition videos,
and all the project files used in the training.
Your purchase will help to create more free courses like this in the future.