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

Python Ch2

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

Python Ch2

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

CHAPTER 2

Control flow &


Function in Python

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
In our daily life we do many things which
depends on some kind of conditions for e.g. If
I study I will pass exams.
So in programming language also we can
perform different sets of actions depending
on the circumstances.
There are three major decision making
instructions:
1) If statements
2) If else statements
3) If-elif-else ladder
4) Nested if Statements

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
if statement
 The if statement is used to
test a particular condition and
if the condition is true, it
executes a block of code
known as if-block.
The condition of if statement
can be any valid logical
expression which can be either
evaluated to true or false.

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
if statement
 The if statement is used to test a particular condition
and if the condition is true,
 The syntax of the if-statement is given below.
if expression:
statement
Example
num = int(input("enter the number:-"))
if num%2 == 0:
print("Number is even")
Output:
enter the number:- 10
Number is even
Ashika Pokiya Dept. of Computer Engineering LJ
Conditional Statements
If-else statement
 The if-else statement
provides an else block
combined with the if
statement which is executed in
the false case of the condition.

 If the condition is true, then


the if-block is executed.
Otherwise, the else-block is
executed.

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
if statement

 The syntax of the if-else statement is given below.

if condition:
#block of statements
else:
#another block of statements (else-block)

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
If-else statement
Example: Program to check whether a person is eligible to
vote or not.
age = int (input("Enter your age:- "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");

Output:
Enter your age:- 90
You are eligible to vote !!
Ashika Pokiya Dept. of Computer Engineering LJ
Conditional Statements
If-elif-else statement
 The elif statement enables us to check multiple
conditions and execute the specific block of statements
depending upon the true condition among them. We
can have any number of elif statements in our program
depending upon our need. However, using elif is
optional.

 The elif statement works like an if-else-if ladder


statement in C. It must be succeeded by an if statement.

Ashika Pokiya Dept. of Computer Engineering LJ


Conditional Statements
If-elif-else statement
 The syntax of the elif
statement is given below.

if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Ashika Pokiya Dept. of Computer Engineering LJ
Conditional Statements
If-elif-else statement
Example:
number = int(input("Enter the number:- "))
if number==10:
print("number is equals to 10")
elif number==50:
print("number is equal to 50");
elif number==100:
print("number is equal to 100");
else:
print("number is not equal to 10, 50 or 100");
Output:
Enter the number:- 15
number is not equal to 10, 50 or 100
Ashika Pokiya Dept. of Computer Engineering LJ
Conditional Statements
Nested-If statement
Python while loop is just another Python
statement. As you already know that while loop body
can contain statements.
we can write while loop inside while loop. While
loop inside another while loop is called Nested While
Loop.
Syntex:-
#statement(s)
while condition_1 :
#statement(s)
while condition_2 :
#statement(s)
Ashika Pokiya Dept. of Computer Engineering LJ
Conditional Statements
Nested-If statement

Example:
i=1 Output:
while i <= 4 : 1111
j=0 2222
while j <= 3 : 3333
print(i, end=" ") 4444
j += 1
print()
i += 1

Ashika Pokiya Dept. of Computer Engineering LJ

You might also like