Your programming assistant #2: deep dive into conditional statements
Conditional statements in programming languages allow you to execute a block of code only if a certain condition is met. In the article about control flow we saw the basics of it, but we can use conditions in some different forms as well. So let's look at some of them.
Nested conditions
Conditional statements nesting refers to the practice of placing one conditional statement within another. Here’s an example in Python:
x = 10
y = 20
if x > 5:
if y > 15:
print("x is greater than 5 and y is greater than 15")
else:
print("x is greater than 5 and y is not greater than 15")
else:
print("x is not greater than 5")
In this example, we have two conditions to check: whether x
is greater than 5, and whether y
is greater than 15. The inner if
statement checks whether y
is greater than 15, and the outer if
statement checks whether x
is greater than 5.
If both conditions are true, the program will print “x is greater than 5 and y is greater than 15”. If x
is greater than 5 but y
is not greater than 15, the program will print "x is greater than 5 and y is not greater than 15". If x
is not greater than 5, the program will print "x is not greater than 5".
Ternary operators
A ternary operator is a shorthand way to write simple if-else statements in some programming languages. Here’s an example in Ruby:
x = 10
y = 20
result = x > 5 ? "x is greater than 5" : "x is not greater than 5"
puts result
result = y > 15 ? "y is greater than 15" : "y is not greater than 15"
puts result
In this example, we use the ternary operator to determine the value of the result
variable based on two conditions: whether x
is greater than 5, and whether y
is greater than 15.
The basic syntax of a ternary operator in most modern languages is condition ? expression1 : expression2
, where expression1
is executed if the condition is true, and expression2
is executed if the condition is false.
Switch Statements
A switch statement is a way to perform conditional logic in some programming languages. It’s often used as an alternative to a series of if-else statements, especially when you have many cases to consider. Here’s an example in JavaScript:
const x = 10;
switch (x) {
case 5:
console.log("x is 5");
break;
case 10:
console.log("x is 10");
break;
default:
console.log("x is not 5 or 10");
break;
}
In this example, the switch statement checks the value of x
, and executes a different block of code based on it. If x
is equal to 5, the program will log "x is 5". If x
is equal to 10, the program will log "x is 10". If x
is neither 5 nor 10, the program will log "x is not 5 or 10".
Each case in the switch statement is a possible value for x
, and the code following each case is executed if x
is equal to that value. The break
statement is used to stop the execution of the switch statement once a case has been executed. If you don't include a break
statement, the switch statement will continue to execute the code for each subsequent case, even if a match has been found.
Short-circuit evaluation
Short-circuit evaluation is a feature of some programming languages that allows you to skip the evaluation of an expression if its outcome can be determined without evaluating the entire expression. Here’s an example in PHP:
$x = 10;
$y = 0;
if ($x > 5 && $y != 0) {
// this block of code will not be executed
// because $y is equal to 0
}
In this example, we use the &&
operator to check if both $x
is greater than 5 and $y
is not equal to 0. Because $y
is equal to 0, there's no need to evaluate the second part of the expression ($y != 0
).
Short-circuit evaluation is a useful feature because it can help you write more efficient and readable code, and it can help you avoid potential errors and warnings in your program. However, it’s important to be mindful of the order of operations when using short-circuit evaluation, and to use parentheses to control the order of evaluation if necessary.
Know your falsy values
Falsy values are values that are considered to be “false” in conditional statements. In most programming languages, the following values are considered falsy:
0
(integer zero)""
(empty string)false
(the booleanfalse
)null
(null value)
Any other value, including all non-zero integers, all non-empty strings, and the boolean value true
, is considered truthy. Among the topics of this article, falsy values are the most likely ones to have specificities in each language, so keep that in mind when writing conditions.
Here’s an example of falsy values in shell script:
#!/bin/bash
x=0
if [ $x ]; then
echo "x is truthy"
else
echo "x is falsey"
fi
x=""
if [ $x ]; then
echo "x is truthy"
else
echo "x is falsey"
fi
x=false
if [ $x ]; then
echo "x is truthy"
else
echo "x is falsey"
fi
In this example, the [ $x ]
expression is used to test if $x
is truthy or falsey. If $x
is truthy, the expression will evaluate to true
, and the code in the if
block will be executed. If $x
is falsey, the expression will evaluate to false
, and the code in the else
block will be executed.
Note that I intentionally added each example in a different language. That's because we are talking about very basic programming concepts here, so I wanted to demonstrate how syntax will not be in the way of understanding those.