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

6-Conditional Blocks Using If, Else and Elif-13-03-2023

The document discusses different control structures in programming languages including sequence, selection, iteration, and branching. It provides details on conditional constructs using if/else statements with examples. It also lists sample programs of varying difficulties using if/else conditional logic.

Uploaded by

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

6-Conditional Blocks Using If, Else and Elif-13-03-2023

The document discusses different control structures in programming languages including sequence, selection, iteration, and branching. It provides details on conditional constructs using if/else statements with examples. It also lists sample programs of varying difficulties using if/else conditional logic.

Uploaded by

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

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
1. SEQUENCE – FLOW CHART

Statement 1

Statement 2

Statement 3
1. SEQUENCE - PROGRAM
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():
2. SELECTION

SELECTIO
N
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.
CONDITIONAL CONSTRUCT – if else STATEMENT
CONDITIONAL CONSTRUCT – if else STATEMENT

Conditional constructs (also known as if


statements) provide a way to execute a chosen block of
code based on the run-time evaluation of one or more
Boolean expressions. In Python, the most general form
of a conditional is written as follows:

Contd.. Next Slide


CONDITIONAL CONSTRUCT – if else
STATEMENT

: Colon Must

if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
CONDITIONAL CONSTRUCT – if else
STATEMENT

FLOW CHART
CONDITIONAL CONSTRUCT – if else
STATEMENT

FLOW CHART

False
Condition ? Statement 1 Statement 2

Main True
Body
Statement 1

else
Statement 2 body
CONDITIONAL CONSTRUCT – if else
STATEMENT

• Each condition is a Boolean expression, and


each body contains one or more commands that
are to be executed conditionally.

• If the first condition succeeds, the first body will


be executed; no other conditions or bodies are
evaluated in that case.
CONDITIONAL CONSTRUCT – if else
STATEMENT

• If the first condition fails, then the process


continues in similar manner with the evaluation
of the second condition. The execution of this
overall construct will cause precisely one of the
bodies to be executed.

• There may be any number of elif clauses


(including zero), and
• The final else clause is optional.
CONDITIONAL CONSTRUCT – if else
STATEMENT

EXAMPLE - PROGRAM
EXAMPLES – if STATEMENT

else is missing,
it is an
optional
statement

OUT
PUT
CONDITIONAL CONSTRUCT

EXAMPLE – if else STATEMENT


EXAMPLE – if else STATEMENT

: Colon Must

else is
used

OUT
PUT
CONDITIONAL CONSTRUCT

EXAMPLES – if elif STATEMENT


EXAMPLES – if elif STATEMENT

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

OUTPU
T
PROGRAM LIST ON if CONTSTUCT
PROGRAM LIST ON if CONTSTUCT

BELOW AVERAGE PROGRAMS

1. Write a PYTHON program that reads a value of n


and check the number is zero or non zero value.
2. Write a PYTHON program to find a largest of
two numbers.
3. Write a PYTHON program that reads the number
and check the no is positive or negative.
4. Write a PYTHON program to check entered
character is vowel or consonant.
PROGRAM LIST ON if CONTSTUCT

AVERAGE PROGRAMS
5. Write a PYTHON program to evaluate the
student performance
If % is >=90 then Excellent performance
If % is >=80 then Very Good performance
If % is >=70 then Good performance
If % is >=60 then average performance
else Poor performance.
6. Write a PYTHON program to find largest of three
numbers.
7. Write a PYTHON program to find smallest of
three numbers
PROGRAM LIST ON if CONTSTUCT

ABOVE AVERAGE PROGRAMS


8.Write a PYTHON program to check weather
number is even or odd.
9.Write a PYTHON program to check a year for
leap year.
10. A company insures its drivers in the following
cases:
- If the driver is married.
- If the driver is unmarried, male and above 30
years of age.
- If the driver is unmarried, female and above 25
years of age.
PROGRAM LIST ON if CONTSTUCT

ABOVE AVERAGE PROGRAMS

In all the other cases, the driver is not insured.


If the marital status, sex and age of the driver are
the inputs,

Write a PYTHON program to determine


whether the driver is insured or not

***

You might also like