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:
- Conditional/Selection statements
- Iteration/Loop statements
- 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=int(input("Enter age: "))
if age>=18:
eligibility=True
else:
eligibility=False
print("Eligibility status: ",eligibility)
Eligibility of vote program with simple if else
Else-if statement
If we want to test a particular case through multiple conditions, we can do it using the else-if statement.
if marks>=85:
grade='A+'
else if marks>=65:
grade='B+'
else if marks>=45:
grade='c+'
else:
grade="FAIL"
print("Grade: ",grade)
Grades of a student program using Elif statements
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.
if num>0:
print("Positive number")
if num<10:
print("One digit number")
else if num<100:
print("Two digit number")
else if num<1000:
print("Three digit number")
else:
print("Negative Number")
Program to display the number is positive or negative
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.
Syntax: for variable_name in sequence
x = ["joe",69,6.9]
for(int i; i < len(x); i++)
print(x[i])
Variable that acts as one element of the list
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).
control_var = initial_value
while condition:
#Code
Update_expression
Syntax for while loop
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.
i = 1
while i <= 5:
print("hello world")
i += 1
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)
Program with break statement
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)
Program with continue statement
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)
Program with pass statement