0% found this document useful (0 votes)
10 views

Control Statements

The document discusses various control flow statements in Python including if/else statements, nested if statements, while loops, for loops using range, break, continue, pass, and else statements.

Uploaded by

misthimangulley
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Control Statements

The document discusses various control flow statements in Python including if/else statements, nested if statements, while loops, for loops using range, break, continue, pass, and else statements.

Uploaded by

misthimangulley
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CONTROL FLOW STATEMENTS

CONDITIONS IN PYTHON(IF
STATEMENT)

• Conditional Statements in Python perform different computations or actions


depending on whether a specific Boolean constraint evaluates to true or false.
• Conditional statements are handled by IF statements in Python.
• Python if Statement is used for decision-making operations.
• It contains a body of code which runs only when the condition given in the if
statement is true. If the condition is false, then the optional else statement runs which
contains some code for the else condition.
IF...ELSE STATEMENT

• if Statement Syntax:

if expression:
Statement
else:
Statement
NESTED IF EXAMPLE

age = int(input("How old are you? "))


if age < 21:
print("You are not allowed to go in the club")
print("You need to wait for", (21-age), "years")
else:
if age > 65:
print("You are not allowed to go in the club")
else:
print("You are allowed to go in the club")
WHILE LOOP

• Python has two primitive loop commands:


• While loop:
• With the while loop we can execute a set of statements as long as a
condition is true.

i=0
while i < 10:
print(i)
i += 1
WHILE LOOP

• Write a program to accept a number and print a table of a number


WHILE LOOP

• Write a program to accept a number and print a table of a number

a=int(input(“Enter a number”))
i=1
while i<=10:
p=a*i
print(p)
i=i+1
WHILE LOOP

• Write a program to accept a number and print a factorial of a number


WHILE LOOP

• Write a program to accept a number and print a factorial of a number


n=int(input(“Enter a number”))
i=1
while i<=n:
f=f*i
i=i+1
print(f)
WHIE LOOP WITH ELSE

• else Statement with while:


• With the else statement we can run a block of code once when the condition no
longer is true:

i=0
while i <= 10:
print(i)
i += 1
else:
print("we have already printer 0 to 10")
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).
• It works more like an iterator method as found in other
object-orientated programming languages like java
• With the for loop we can execute a set of statements, once
for each item in a list, tuple, set etc.
• E.g.
students = ["max", "james", "henry"]
for x in students:
print(x)
RANGE FUNCTION IN FOR LOOP

• To loop through a set of code a specified number


of times, we can use the range() function
• The range() function returns a sequence of
numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a
specified number.
• for x in range(10):
print(x)
FOR LOOP WITH RANGE

• Write a program to accept a number and print a table of a number


RANGE FUNCTION IN FOR LOOP

• The range() function defaults to 0 as a starting value, however it is


possible to specify the starting value by adding a parameter: range(4,
10), which means values from 4 to 10 (but not including 10)
• for x in range(4, 10):
print(x)
RANGE FUNCTION IN FOR LOOP

• The range() function defaults to increment the sequence by 1, however it


is possible to specify the increment value by adding a third parameter:
range(4, 20, 2)
• for x in range(4, 20, 2):
print(x)
• Increments the sequence from 4 till 20 by 2
ELSE IN FOR LOOP

• The else keyword in a for loop specifies a block of code to


be executed when the loop is finished
for x in range(6):
print(x)
else:
print("Finally finished!")
B R E A K S TAT E M E N T

• With the break statement we can stop the loop even if the while/for condition is
true

Ex. 2 for x in range(10):


if x == 5:
Ex.1 break
i=0 print(x)
while i <=10:

if i == 5:
break
print(i)
i += 1
THE CONTINUE STATEMENT

• With the continue statement we can stop the current


iteration, and continue with the next:
i=0
while i <= 10:
if i == 5:
continue
print(i)
i += 1
PASS STATEMENT

• The Python pass statement is a null statement. But the


difference between pass and comment is that comment is
ignored by the interpreter whereas pass is not ignored.
• When the user does not know what code to write, So user
simply places a pass at that line.
• If we do not use pass or simply enter a comment or a blank
here, we will receive an IndentationError error message.
• n = 26
• if n > 26:
• # write code your here
• print('Geeks')
PASS STATEMENT-EXAMPLE

li =['a', 'b', 'c', 'd']

for i in li:
if(i =='a'):
pass
else:
print(i)

You might also like