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:
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.
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=28if age>=18:eligibility=Trueelse:eligibility=Falseprint("Eligibility status: ",eligibility)
Else-if
statementIf we want to test a particular case through multiple conditions, we can do it using the else-if statement.
marks = 87if 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.
if-else
When there is another block of if-else statements inside an outer block of if-else, is called nested if-else.
num = 80if 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 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
loopA For
loop works on
x = ["joe",69,6.9]for i in range(len(x)):print(x[i])
While
loopsWhile
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 itwhile 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 thewhile
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 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 statements are used in loops to terminate execution when encountered.
for i in range(10):if (i==5):breakprint(i)
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):continueprint(i)
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:
for i in 'Welcome':if(i == 'e'):print('pass executed')passprint(i)print('<======>')for i in 'World':if(i == 'e'):print('continue executed')continueprint(i)