05. Control Statements

Control Statements

The Control flow statement are used to control the execution flow of an executable program.The statements inside your program are generally executed from top to bottom,in the same order they are written,Control flow statements break up the flow of execution by employing decision making , looping , and branching statements.

Note: the colon (:) following <condition> is required. Some programming languages require <condition> to be enclosed in parentheses, but Python does not.

if statement

It is the most basic control statement of all the control flow statements.The if statement conditionally executes a single statements or block of code.

For Example :

a = 560

b = 200

if a > b :

print("A is greater number ")

Output

A is greater number

if else statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false. The if-else statement provides a secondary path of execution when if clause evaluates to false.

For Example :

marks=75if(marks > 80 ):

print("Grade A")

else:

print("Grade B")

Output

Grade B

Loops

The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the

following diagram to understand the workings of a loop statement.

There are two types of loops used in python are

For loop

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

For Example :

# Multiplication table using for loop

n = int(input("Enter number = "))

for i in range(1,11):

print(n,'x',i,'=',n*i)

Output

Enter number = 5

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

#Second program to iterate over characters of a string using loop

string_name = "helloworld"

for element in string_name:

     print(element)

string_name = "HELLO"

# Iterate over index

for element in range(0, len(string_name)):

     print(string_name[element])

Output

h

e

l

l

o

w

o

r

l

d

H

E

L

L

O

while loop

The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

For Example:

i = 0

while i<10:

    print ( ' Hello ',i)

    i +=1

Output

Hello 0

Hello 1

Hello 2

Hello 3

Hello 4

Hello 5

Hello 6

Hello 7

Hello 8

Hello 9

Break statement

The break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.In other words, we can say that break is used to abort the current execution of the program and the control goes to the next line after the loop

For Example:

str = "python"

for i in str:

    if i == 'o':

        break

    print(i);

Output:

p

y

t

h

Continue Statement

The continue statement in python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.

For Example:

i=1; #initializing a local variable

for i in range(1,11):

    if i==5:

        continue;

    print("%d"%i);

Output:

1

2

3

4

6

7

8

9

10