Docsity

Control Flow - Python Programming, Lecture notes of Programming Languages

Toggle sidebar
bg1
Unit III Control Flow
The statement in the program is executed one by one
is called Sequential execution.
Control statement are the statements which control or
change the flow of the execution. OR Condition is statement
that is either True or False.
Python uses conditions to make decision and execute a
block of code
Used with relational and logical operators
Examples: x >
10
a == b
age =>18
name ==
“karma”
Ngawang
T
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Control Flow - Python Programming and more Lecture notes Programming Languages in PDF only on Docsity!

Unit III Control^ Flow

The statement in the program is executed one by one

is called Sequential execution.

Control statement are the statements which control or

change the flow of the execution. OR Condition is statement

that is either True or False.

Python uses conditions to make decision and execute a

block of code

Used with relational and logical operators

Examples: x >

a == b

age =>

name ==

“karma”

IF statement

It is used to execute one or more statement depending on whether a condition is TRUE or NOT. The IF statement execute a block of code only if the condition is TRUE. SYNTAX:

If

condition:

statem ent/ Block of code Conditio n True? Execute code False Indentation is mandatory

Exampl

e:

x = 10 if x >5: pri nt( “ x is True Output: x is greater than 5 Output Ngawang

IF ELIF ELSE statement

The IF ELIE ELSE statement allows us to check multiple condition. Only one block of code is executed. SYNTAX: **if condition1:

code block 1

elif condition2: #code block 2 else: #code block 3 True False** Conditio n 1 True? Execute code 1 Conditio n 2 True? False Execute code 3 Output True Execute code 2

Examples:

x = 7 if x % 2 = = 0: print(“Even Number”) else: print(“Odd number”) OR n1 = int(input(“ Enter the Number”)) if n1 % 2 = = 0: print(“Even Number”) else: print(“Odd number”) Marks = 70 if Marks > = 90 : print(“ Grade A”) elif Marks > = 90 : print(“ Grade B”) elif Marks > = 90 : print(“ Grade C”) else: print(“ Grade D”)

Cont.…………

SYNTAX for^ **loop while condition:

code to execute**

Example: x = 1 while x < = 10 : print(x) x+= print(“End”) It is used to iterate over the element of a sequence. It means for loop can be used to execute a group of statement repeatedly depending upon the number of element in the sequence. It can work with sequences like List, Tuple, array etc Best when you know how many time to repeat.

Cont.…………

SYNTAX

Nested Loops It is possible to write one loop inside another loop. **for variable in sequence:

code to execute**

Example: for i in range(3): for j in range(4): print(“ i ” = i, \t, ” j ” = j ) for i in range(1,6): print( i ) Output: 1,2,3,4, Program: A python program that display star in right angled triangle form using nested loop.

for i in range(1,5): for j in range(1, i + 1): print(“ * “, end = “ ” ) print()

Cont.…………

Loop Control statement

The statement changes the normal flow of loops.

Break statement

 stops the loop completely.

 (^) Control move to the next statement after the loop.

Example: for^ i^ in^ range(1,6):

if i = = 3 : brea k print( i

Output: 1,

Ngawang

Cont.…………

Continue statement

It is used in a loop to go back to the beginning of the loop. It means when the continue is executed, the next repetition will start and the subsequent statement in the loop are not executed.

Example:

for i in range(1,6): if i = = 3 : continu e print( i )

Output: 1, 2,4,

THANK YOU