KEMBAR78
Mod 2 Conditional Statements | PDF | Control Flow | Computer Science
0% found this document useful (0 votes)
6 views23 pages

Mod 2 Conditional Statements

Uploaded by

nitinkumar547413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views23 pages

Mod 2 Conditional Statements

Uploaded by

nitinkumar547413
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Module 2

SEQUENCE SELECTIO ITERATION


N
TYPES OF CONTROL STRUCTURES
A Structured programming is an
important feature of a programming
language which comprises following
logical structure:
1. SEQUENCE

2. SELECTION

3. ITERATION OR LOOPING

4. BRANCHING OR JUMPING STATEMENTS


1. SEQUENCE

Sequence is the default control


structure; instructions are executed
one after another.

Statement 1
Statement 2
Statement 3
……..
……..
……..
1. SEQUENCE – FLOW CHART

Statement 1

Statement 2

Statement 3
SEQUENC
E
1. SEQUENCE - PROGRAM

Sequence is the default control


structure; instructions are executed
one after another.
# This program adds two numbers
def sum_of_two_no():
num1 = 1.5
num2 = 6.3
sum = float(num1) + float(num2)
print('The sum is =‘, sum)
sum_of_two_no():
CONTROL STRUCTURES
2. SELECTION

A selection
statement causes
the program
control to be
transferred to a
specific flow based
upon whether a
certain condition is
true or not. SELECTION
CONDITIONAL CONSTRUCT – if STATEMENT
 If statement is the most simple
form of decision-making statement. It
takes an expression and checks if the
expression evaluates to True then the block
of code in if statement will be executed.
 If the expression evaluates to False, then
the block of code is skipped.
Note: Don’t forget to add a colon(:) after if
statement and indent the statements
properly that are executed when a condition
is True.
CONDITIONAL CONSTRUCT – if STATEMENT

SYNTAX
if condition:
statements
INDENTATION IN PYTHON
EXAMPLE
a = 20 ; b = 20
if ( a == b ):
print( “a and b are equal”)
a and b are equal
num = 5
if ( num >= 10):
print(“num is greater than 10”)
print(“condition fails”)
condition fails
CONDITIONAL CONSTRUCT – if else STATEMENT

The if-else statement checks the


expression and executes the if block
when the expression is True otherwise
it will execute the else block of code.
The else block should be right after if block
If the expression is
and it is executed when
statement
False. If condition:

statements
else:
CONDITIONAL CONSTRUCT – if else STATEMENT

FLOW CHART

False
Condition ? Statement 1 Statement 2

Main True
Body
Statement 1

else
Statement 2 body
EXAMPLES – if STATEMENT

OUT PUT
EXAMPLE – if else STATEMENT

: Colon
Must

else is
used

OUT PUT
EXAMPLES – if elif STATEMENT

READ AS
18 is less
than age
and
18 is less
than 60

OUTPUT
Nested – if STATEMENT

Nested if statements is an if statement


inside another if statement. Python
allows us to stack any number of if
statements inside the block of another if
statements. They are useful when we
need to make a series of decisions.
Nested – if STATEMENT Syntax
E L IG IB IL IT Y F O R A A D H A R C A R D -N E S T E D IF S T A T E M E N T

age = int(input("Enter your age : ")) Output:


1st Run
if age > 5: Enter your age : 23
nationality = input("Enter your NationalityEnter
: ") your Nationality :
INDIAN
if nationality.upper() == 'INDIAN' : You are eligible for Aadhar
card
print("You are eligible for Aadhar card")
2nd Run
else: Enter your age : 4
Age criteria not met
print("Not for other nationalities")
3rd Run
else: Enter your age : 15
Enter your Nationality :
print("Age criteria not met") GERMAN
Not for other nationalities
Multi-way – if STATEMENT

 In Python, we have an elif keyword to chain


multiple conditions one after another. With
elif ladder, we can make complex decision-
making statements.
 The elif statement helps us to check
multiple expressions and it executes the
code as soon as one of the conditions
evaluates to True.
EXAMPLE
print(‘Select your Course:’)
print(‘1. CSE’)
print(‘2. AI’)
print(‘3. DS’)
Select your ride:
choice = int( input() )
1. CSE
if(choice == 1): 2. AI
print(‘You have selected CSE’) 3. DS
3
elif(choice == 2):
You have selected
print(‘You have selected AI’) DS
elif(choice == 3):
print(‘You have selected DS’)
else:
print(‘Wrong choice!’)
THANK YOU

You might also like