Python Control Statements

Control statements in Python are used to control the flow of execution of a program. They allow the programmer to specify which statements should be executed and when they should be executed based on specific conditions. Control statements are an important feature of any programming language, and Python provides a rich set of control statements to enable programmers to write complex and efficient code.

In this article, we will cover the three main types of control statements in Python – conditional statements, loops, and control flow statements – and provide examples of their usage in different scenarios.

Conditional Statements:

Conditional statements in Python are used to execute a specific block of code only if a certain condition is met. The most commonly used conditional statement in Python is the “if” statement. The syntax for an “if” statement is as follows:

if condition:
    statement1
    statement2
    ...
    statementn

The “condition” in the above syntax is any expression that can be evaluated as True or False. If the condition is True, then the statements within the “if” block will be executed. If the condition is False, then the statements within the “if” block will be skipped.

Here is an example of an “if” statement:

x = 10
if x > 5:
    print("x is greater than 5")

In the above example, the condition “x > 5” is True, so the statement “x is greater than 5” will be printed.

Another type of conditional statement in Python is the “if-else” statement. This statement is used to execute one block of code if the condition is True, and a different block of code if the condition is False. The syntax for an “if-else” statement is as follows:

if condition:
    statement1
    statement2
    ...
    statementn
else:
    statementa
    statementb
    ...
    statementz

In the above syntax, if the condition is True, the statements within the “if” block will be executed, and if the condition is False, the statements within the “else” block will be executed.

Here is an example of an “if-else” statement:

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")

In the above example, the condition “x > 5” is False, so the statement “x is less than or equal to 5” will be printed.

Nested if-elif-else statements in Python are a more complex version of nested if-else statements, which allow for the evaluation of multiple conditions with multiple possible outcomes. This type of statement is useful when there are multiple conditions that need to be evaluated in a hierarchical manner, and each condition has multiple possible outcomes.

The syntax for a nested if-elif-else statement is as follows:

if condition1:
    statement1
elif condition2:
    statement2
    if condition3:
        statement3
    elif condition4:
        statement4
    else:
        statement5
else:
    statement6

In the above syntax, “if” and “elif” statements are nested within each other, allowing for the evaluation of multiple conditions with multiple possible outcomes. Here is an example of a nested if-elif-else statement:

x = 5
y = 10
if x > y:
    print("x is greater than y")
elif x < y:
    print("x is less than y")
    if y - x > 5:
        print("The difference between y and x is greater than 5")
    elif y - x > 2:
        print("The difference between y and x is greater than 2")
    else:
        print("The difference between y and x is less than or equal to 2")
else:
    print("x is equal to y")

In the above example, the first “if” statement evaluates whether “x” is greater than “y”. If this condition is False, the first “elif” statement is executed. This statement evaluates whether “x” is less than “y”. If this condition is True, the nested “if-elif-else” statement is executed. This statement evaluates the difference between “y” and “x”, and prints a message based on the size of the difference.

Nested if-elif-else statements can be used to evaluate multiple conditions with multiple possible outcomes in a hierarchical manner, allowing for more complex logic to be implemented within a single block of code. However, as with nested if-else statements, care must be taken to ensure that the nesting does not become too deep, as this can make the code difficult to read and maintain.

Loops:

Loops in Python are used to repeat a specific block of code multiple times. There are two main types of loops in Python – “for” loops and “while” loops. A “for” loop is used to iterate over a sequence of elements, such as a list, tuple, or string. The loop will continue iterating until it has reached the end of the sequence.

The syntax for a “for” loop is as follows:

for variable in sequence:
    statement1
    statement2
    ...
    statementn

In the above syntax, “variable” is a variable that will be assigned to each element in the sequence, and the statements within the loop will be executed for each element in the sequence. Here is an example of a “for” loop:

my_list = [1, 2, 3, 4, 5]
for num in my_list:
    print(num)

In the above example, the loop will iterate over each element in “my_list” and print it to the console.

A “while” loop is used to repeat a block of code until a certain condition is met. The syntax for a “while” loop is as follows:

while condition:
    statement1
    statement2
    ...
    statementn

In the above syntax, the statements within the loop will be executed repeatedly as long as the condition is True. Here is an example of a “while” loop:

x = 0
while x < 5:
    print(x)
    x += 1

In the above example, the loop will continue to print the value of “x” as long as it is less than 5. The value of “x” will be incremented by 1 after each iteration.

Control Flow Statements:

Control flow statements in Python are used to alter the normal flow of execution of a program. There are three main control flow statements in Python – “break”, “continue”, and “pass”.

The “break” statement is used to exit a loop prematurely. It can be used in both “for” and “while” loops. When a “break” statement is encountered, the loop will immediately exit and execution will continue with the next statement after the loop.

Here is an example of a “for” loop with a “break” statement:

my_list = [1, 2, 3, 4, 5]
for num in my_list:
    if num == 3:
        break
    print(num)

In the above example, the loop will iterate over each element in “my_list” and print it to the console. However, when the value of “num” is 3, the “break” statement will be executed and the loop will immediately exit.

The “continue” statement is used to skip to the next iteration of a loop. It can also be used in both “for” and “while” loops. When a “continue” statement is encountered, the current iteration of the loop will be skipped and the loop will continue with the next iteration.

Here is an example of a “for” loop with a “continue” statement:

my_list = [1, 2, 3, 4, 5]
for num in my_list:
    if num == 3:
        continue
    print(num)

In the above example, the loop will iterate over each element in “my_list” and print it to the console. However, when the value of “num” is 3, the “continue” statement will be executed and the current iteration of the loop will be skipped.

The “pass” statement is used to do nothing. It can be used as a placeholder when a statement is required syntactically, but no action is needed. Here is an example of a “if” statement with a “pass” statement:

x = 10
if x > 5:
    pass
else:
    print("x is less than or equal to 5")

In the above example, the “if” statement contains a “pass” statement, which does nothing. If the condition “x > 5” is True, nothing will happen. If the condition is False, the statement “x is less than or equal to 5” will be printed.

In conclusion, control statements in Python are an essential feature for controlling the flow of execution of a program. Conditional statements, loops, and control flow statements are the three main types of control statements in Python. Understanding how to use these statements effectively can help programmers write efficient and complex code.

The codes (if any) mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information

Share this