Compound statements contain other statements. This means a test or execution while true
or false
executes the statements within itself. The trick is to write statements so that they are efficient and effective. Examples of this include if then
statements, loops, and exception handling.
An if
statement tests for a specific condition, and if that condition is met (or not met), then the statement is executed. The if
statement can include a simple check to see whether a variable is true
or false
, and then print the details, as shown in the following example:
x = 1 if x == 1: print("The variable x has a value of 1")
The if
statement can even be used to check for multiple conditions at the same time. Keep in mind that it will execute the first portion of the compound statement that meets the condition and skip the rest. Here is an example that builds on the previous one, using else
and elif
statements. The else statement is a catch all if none of the if
...