Conditional statements in Python
In programming, conditional statements are features of a programming language which performs different actions depending on a predetermined true / false condition. For the most part, this is done through altering the control flow based on some condition or conditions. Switch is the exception to the rule in that it uses branch prediction. Most programming languages have three types of conditional statements: if, if..else if, and switch. Python is different from most other languages because it only implements two: if and if..else if. In functional programming, conditional statements are referred to as conditional expressions or conditional constructs.
If statements
If statements consists of a boolean expression followed by one or more statements. If statements can also be followed by an optional else statement which executes when the boolean expression is FALSE. Python assumes any non-zero and non-null vales as TRUE and if there is either a zero or a null, it is assumed to be FALSE. Java does not make such assumptions since Java treats boolean expressions as a boolean primitive data type. In PHP, only zero and null are treated as false, anything else is considered true including negative numbers and any non null strings. If statements are usually constructed with the keyword if but can also be constructed using ternary operators. Python treats codes blocks differently from Java and PHP in that Python does not use brackets to separate code blocks, it uses a colon and spaces instead. Below are examples of if statements:
Python
l_condition = 1 if ( l_condition == 1 ) : print "Condition is true" else: print "Condition is false"
PHP
$l_condition = 1 ; if ( l_condition == 1 ) { echo "Condition is true" ; } else { echo "Condition is false" ; }
Java
$l_condition = 1 ; if ( l_condition == 1 ) { System.out.print "Condition is true" ; } else { System.out.print "Condition is false" ; }
If..else if
There may be a case when we want to check for another condition after a condition resolves to true. In such a situation, you can use the nested if statement. We can also daisy chain if statements together using if..else if. In Python, we use elif keyword. In PHP, else if becomes elseif. In Java's implementation, they spell out everything and use else if. The same rules apply for the else if as it does for the if statements.
Python
l_condition = 1 if ( l_condition == 1 ) : print "Condition is 1" elif ( l_condition == 2 ) : print "Condition is 2" else: print "Condition is neither 1 or 2"
PHP
$l_condition = 1 ; if ( l_condition == 1 ) { echo "Condition is 1" ; } elseif ( l_condition == 2 ) { echo "Condition is 2" ; } else { echo "Condition is neither 1 or 2" ; }
Java
$l_condition = 1 ; if ( l_condition == 1 ) { echo "Condition is 1" ; } else if ( l_condition == 2 ) { echo "Condition is 2" ; } else { echo "Condition is neither 1 or 2" ; }
Switch statements
A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map. We can think of it as a daisy chain of if..else if statements. Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. The benefits of using a switch statement as opposed to a series of if else if statements is that it is much faster since it only has to look up the evaluated variable/expression once and directly jump to the appropriate branch of code to execute it instead of evaluating each condition sequentially.
Java
int day = 3; String dayString; switch (day) { case 1: monthString = "Sunday"; break; case 2: monthString = "Monday"; break; case 3: monthString = "Tuesday"; break; case 4: monthString = "Wednesday"; break; case 5: monthString = "Thursday"; break; case 6: monthString = "Friday"; break; case 7: monthString = "Saturday"; break; default: monthString = "Invalid day"; break; } System.out.println(dayString);
Python
def switch_day(argument) : switcher = { 1: "Sunday", 2: "Monday", 3: "Tuesday", 4: "Wednesday", 5: "Thursday", 6: "Friday", 7: "Saturday", } print switcher.get(argument, "Invalid day") switch_day( 3 )
Most programming languages have some implementations of conditional statements. Conditional statements are necessary when it comes to a program's ability to execute different commands based on some conditions being met. Python's main difference when it comes to conditional statements is that it doesn't implement switch statements with keywords. Instead, it makes us define the map using it's built in dictionary mappings.