What are control statements?

In general, control statements help with the control flow of the program. But, do we mean by control flow?

Control flow is the order in which the statements execute. For example, the program’s flow is usually from top to bottom(Sequential flow), but what if we want a particular block of code to only be executed when it fulfills a specific condition or wants to run a block of the code a specified number of times? These statements will ensure a smooth and intentional flow of a program.

There are three types of control statements:

  1. Conditional/Selection statements
  2. Iteration/Loop statements
  3. Jump statements

Conditional/selection statements

These control statements help with implementing decision-making in a program. A conditional statement will select the block of statements that will execute based on the given condition. The result of the program depends on the condition.

If the condition provided proves to be true, the result is 1; if it proves to be false, the result is 0. When there is a truth value, the code will get executed; otherwise, it will not.

Simple if-else

This code shows a true block and a false block. If the condition is true, the control will pass to the statements written under the if block. If the condition is false, it will pass to statements under the else block.

age=28
if age>=18:
eligibility=True
else:
eligibility=False
print("Eligibility status: ",eligibility)

Else-if statement

If we want to test a particular case through multiple conditions, we can do it using the else-if statement.

marks = 87
if marks >= 85:
grade = 'A+'
elif marks >= 65:
grade = 'B+'
elif marks >= 45:
grade = 'C+'
else:
grade = "FAIL"
print("Grade:", grade)

The program above checks the marks according to the condition we have assigned to the value of variable grade. This is also called an if-else-if ladder.

Nested if-else

When there is another block of if-else statements inside an outer block of if-else, is called nested if-else.

num = 80
if num > 0:
print("Positive number")
if num < 10:
print("One digit number")
elif num < 100:
print("Two digit number")
elif num < 1000:
print("Three digit number")
else:
print("Negative Number")

Iterative statement/looping statements

Iterative statements are also known as repetitive statements or looping statements. A looping statement is used when the program requires a statement’s execution (or the repeated execution of a set of statements) until some condition for loop termination is satisfied.

There are two types of loops:

  • For loop

  • While loop

NOTE: Unlike other languages (like Java, C++, and C), Python does not have the do-while loop.

For loop

A For loop works on iterator objectsobjects that have countable values or whose values can be traversed through. For loops work with sequences such as list, tuple, strings, etc.

x = ["joe",69,6.9]
for i in range(len(x)):
print(x[i])

While loops

While loops depend on a condition. We use this type of loop when we don’t know the exact number of times a loop will run (unlike in For loop).

x = ["joe", 69, 6.9]
i = 0 # Initialize the counter before using it
while i < len(x):
print(x[i])
i = i + 1

If there is a loop variable that will act like a counter and increment for the specified times for the while loop, the loop variable has to be initialized before being used in the while loop’s condition. For the variable to be updated, it has to be manually written ( i.e., (update_expression)) inside the while loop.

Jump statements

Jump statements are statements through which we can transfer control anywhere in the program. These statements will transfer program control by skipping statements that don’t have any specified conditions. These statements are:

1.Break

2.Continue

3.Pass

Break statement

Break statements are used in loops to terminate execution when encountered.

for i in range(10):
if (i==5):
break
print(i)

Continue statement

When encountered, a continue statement skips the rest of the statements to be executed after it in the current loop, and the control goes back to the next iteration. This means that statements encountered after the continue statement is encountered won’t be executed for the current iteration.

Continue can only be used in a looping control statement.

for i in range(6):
if(i==3):
continue
print(i)

Pass statement

The pass statement is a Null(empty) statement – it has no logical output. If we have to create an empty if...else statement or an empty method, we can write the pass statement and execute it.

Pass vs. continue:

  • Continue has semantic meaning and a logical impact on the flow of control
  • Pass does nothing except act as a comment, i.e., it won’t be ignored, but it doesn’t have any effect
  • Continue will shift the control back to the start of the loop for the next iteration; so, it won’t execute any remaining statement below the continue statement.
for i in 'Welcome':
if(i == 'e'):
print('pass executed')
pass
print(i)
print('<======>')
for i in 'World':
if(i == 'e'):
print('continue executed')
continue
print(i)

Free Resources