Bona Akubue | Software Developer

control statements in python

Control statements in python

Control statements, also known as control structures are important building blocks in programming, and in this tutorial will learn how to use them for problem-solving in Python.

Programs are usually executed sequentially from left to right and from the top to the bottom. However, with control statements, you can alter the flow of program execution, allowing you to execute whichever code in any order you wish.

You can decide to prevent or skip certain lines of code from executing or even make the last line of code executed before the preceding ones.

Also, you may also want to repeat a given piece of code over and over again.

Python Control Statements

Real-life programs are composed of one or a combination of different types of control statements.

Essentially, there are three types of control statements you can use in Python, and they include:

  • Selection or Conditional statements
  • Looping or Iteration statements
  • Jump statements

Selection or Conditional statements

Selection or conditional statements are used to specify or indicate statements to be executed if certain conditions are met. In a situation, where the condition is not met, an alternative course of action may be provided.

Examples of selection or conditional statements include:

  • if statements
  • if else statements
  • if elif else statements

if statements

This is one of the most popular of all the control statements. It is used to instruct the computer to perform an action if a certain condition is True or skip the action if the condition is False.

The syntax for is statement is:

if (conditional test):

    do something

Let’s say that you want to determine whether a number is an even number, and print a message like – this is an even number.

You can easily do this with an if statement.

Since an even number is a number divisible by 2 without a remainder, you can perform a conditional test, using the modulus operator %, to determine whether the number is an even number or not.

If the test’s outcome of the test is True, the message is printed.


num = 14

if num % 2 == 0:
    print('even number')

However, if the number in question is an odd number, nothing happens. To, provide a different course of action to perform if the conditional test turns out False, you need an else statement.

if else statement

Unlike the if statement which only performs an action if a certain condition is met, the if else statement, allows you to specify a different action to perform if the condition is False.

In this case, if the number is not an even number, it should display – odd number.


num = 7
if num % 2 == 0:
    print('even number')
else:
    print('odd number')

if elif else statement

This statement allows you to provide multiple conditions and various actions to perform.

What if you want to perform more than one conditional test and provide an action to perform if any of them turns out True?

This is where the elif statements come in. The elif in Python is like else if in other programming languages like C++ and Java.

Consider the example below:


num = 3
if num % 2 == 0:
    print('divisible by 2')
elif num % 3 == 0:
    print('divisible by 3')
else:
    print('not divisible by 2 and 3')

Iteration or Looping statements

Looping statements, also known as iteration statements are used to repeat a given piece of code a given number of times.

Sometimes, they can even be used to cause an infinite repetition. Looping statements allow you to specify an action that should be repeated and the number of times it should be repeated.

Examples of looping statements are:

  • for loop
  • while loop

for loop

The for loop is the most widely used looping statement in Python programming. For example, you can use the for loop to iterate over items in a list.


numbers = [1,2,3,4,5]

for num in numbers:
    print(num)

#outputs
#1
#2
#3
#4
#5

While loop

The while loop is also another powerful way of performing loops in Python. The example below uses the while loop to print out numbers from 1 to 10.


num = 1

while num < 11:
    print(num)
    num+=1

#outputs
#1
#2
#3
#4
#5
#6
#7
#8
#9
#10

Jump Statements

These are statements that are used to stop or skip certain lines of code from executing, especially in conditional or looping statements.

Examples of jump statements include:

  • break
  • continue
  • pass

break

This is used to stop or terminate an ongoing loop as shown below:


for num in range(5):
    print(num)
    if num == 2:
        break
   
#output
#0
#1
#2

continue

Unlike the break that terminates the loop, continue skips actions at the current value of a loop.


for num in range(5):
    if num == 2:
        continue
    print(num)
   
#output
#0
#1
#3
#4

pass

It is a way of telling Python, especially in a looping operation to do nothing.

Thus, instead of leaving the body of a control statement empty (which is syntactically wrong), you can use the pass keyword instead.

if num % 2 == 0:
    pass

 

Leave a Comment

Your email address will not be published. Required fields are marked *