control statements in python.pptx

4,197 views 13 slides Aug 12, 2023
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

This ppt is about the control statements in python programming language


Slide Content

Control statements in python

CONTROL s TATEMENTs Defination : Control statements are the statements which control or change the flow of execution .

The following are the control statements available in python: If statements If….else statement If…. elif …else statement While loop For loop Break statement Continue statement Pass statement Assert statement Return statement

The if statement: This statement is used to execute one or more statement depending up on whether a condition is true or not. Syntax: if condition: statements code : num=1 output: if num==1: one print(“one”)

The if…else statement : This statement execute the group of statements when a condition is true, otherwise it will execute the else statement. Syntax: if condition: statements else: statements code : x=int(input(“enter the value”)) if x%2==0: print(“even”) else: print(“odd”)

The if.. elif ..else statement : The if.. elif ..else statement is used to execute the block of statements among more than two condition. Syntax: if condition1: code: num=5 statements1 if num==0: elif condition2: print(“zero value”) statement2 elif num<0: elif condition3: print(“negative”) statement3 else: else: print(“positive”) statement4

The while loop: The while loop is useful to execute a group of statement several times repeatedly depending up on whether a condition is True or False. Syntax: while condition: statements code: x=1 while x<=10: print(“x=”,x) x+=1

The for loop: The for loop is useful to iterate over the elements of a sequence.it means ,the for loop can be used to execute a group of statement repeatedly depending upon the number of element in the sequence syntax: for var in sequence: statements code: str=“hello” for ch in str: print( ch )

The break statement: The break statement can be used Inside the a for loop or while loop to come out of the loop. When ‘break’ is executed, the python interpreter jumps out of the loop to process the next statement in the program. Syntax: while/for condition: statements code: x=0 while x<=10: print(“x=”,x) if x==5: break

The continue statements: The continue statement is used within loop to skip the some part of the current iteration and move in to the next iteration. Syntax: while/for condition: if condition: continue code : x=0 while x<=10: print(“x=”,x) if x==5: continue

The pass statement: The pass statement does not do anything. It is used with ‘if’ statement or inside the loop to represent no operation. We use pass statement when we need a statement syntactically but we do not want to do any operation. The assert statement: The assert statement is useful to check if a particular condition is fulfilled or not. Syntax: assert expression, message code: x=int(input(“enter the value greater than 0=”)) assert x>0, “wrong input entered” print(“you entered:” ,x)

The return statement: A return statement is used to end the execution of the function call and “return” the result. Syntax: def fun() statements return expression code: def cube(x): r=x**3 return r

Thank you
Tags