100% found this document useful (2 votes)
260 views6 pages

UNIT1 - Conditional Statments

The document provides an overview of control statements in Python, focusing on conditional statements such as 'if', 'if...else', and 'if...elif...else'. It explains the syntax and usage of these statements with examples and flowcharts to illustrate decision-making processes in programming. Additionally, it covers nested conditions, allowing for more complex decision structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
260 views6 pages

UNIT1 - Conditional Statments

The document provides an overview of control statements in Python, focusing on conditional statements such as 'if', 'if...else', and 'if...elif...else'. It explains the syntax and usage of these statements with examples and flowcharts to illustrate decision-making processes in programming. Additionally, it covers nested conditions, allowing for more complex decision structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Sanjivani Rural Educational Society’s

SANJIVANI COLLEGE OF ENGINEERING


(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

UNIT-1: Control Statement

Conditional Statement
o A conditional statement as the name suggests itself, is used to handle conditions in
your program.
o These statements guide the program while making decisions based on the
conditions encountered by the program.
o Conditional statements in Python support the usual logical conditions
from mathematics.
o For example:
o Equals: a == b
o Not Equals: a != b
o Less than: a < b
o Less than or equal to: a <= b
o Greater than: a > b
o Greater than or equal to : a >= b
if Statement:
o The if statement is the simplest decision-making statement.
o It is used to decide whether a certain statement or block of statements will be
executed or not.
Syntax:
if condition:
statement1
.
statementn

The statement consists of a header line that ends with the colon character (:) followed by
an indented block.
Example:
a=5
if a > 0:
print(“a is positive”)

Prepared By: Prof. S. S. Gawali


Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

Output:
a is positive

The Boolean expression after the if statement is called condition.


We end the if statement with a colon character (:) and the line(s) after the statement are
indented.
Flowchart:

Explanation: In this code, a variable value assign as 5 then check the condition value of a>0
(i.e 5>0), checks result is true, so the output of the code is “a is positive”.
if….else statement
The if statement alone tells us that if a condition is true it will execute a block of statements and
if the condition is false it won’t. But if we want to do something else if the condition is false, we
can use the else statement with the if statement to execute a block of code when the if condition
is false.
It is used to execute the statement(s) as per two possibilities and the conditions.
Syntax of if…else statement
if condition:
Statement1
.
Statementn
else:
Statement1
.

Prepared By: Prof. S. S. Gawali


Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

Statementn

Since the condition must either be true or false, exactly one of the alternatives will be
executed. The alternatives are called branches, because they are branches in the flow of
execution.
Example:
a=9
if a > 0:
print('a is positive')
else:
print("a is negative")

Output:
a is positive
Flowchart:

Explanation: In this code, a variable value assign as 9, then checks the condition value of a>0
(i.e 9>0). The condition result is true, so the output of the code is “an is positive”.

if….elif….else statement (Ladder and Chain Conditional Statement)


sometimes there are more than two possibilities and we need more than two branches. It is
used to check multiple conditions.

Here, a user can decide among multiple options. The if statements are executed from the top
down.
As soon as one of the conditions controlling the if is true, the statement associated with that if is

Prepared By: Prof. S. S. Gawali


Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final
“else” statement will be executed.
Syntax of if..elif..elif ..else statement

if condition:
Statement1
.
Statementn
elif:
Statement1
.
Statementn
.
.
.
else:
Statement1
.
Statementn

Example:
a=0
if a > 0:
print('a is positive')
elif a<0:
print("a is negative")
else:
print('a is zero')

Output:
a is zero

Flowchart

Prepared By: Prof. S. S. Gawali


Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

Explanation: In this code, a variable value assign as 0 then check the condition value of a>0 (i.e
0>0), condition result is False, then check the next condition value of a<0(i.e 0<0), again condition
result is False, then it display else part statement as output “a is zero”.

Nested Condition
 Nested conditions comprise condition statements contained within the definition
of other condition statements.
 We can use an if statement inside of an if statement. This is known as a nested if
statement.

Example
a=5
if a>=0:
if a==0:
print("a is zero")
else:
print("a is positive")
else:
print("a is negative")

Output:
a is positive

Flowchart:

Prepared By: Prof. S. S. Gawali


Sanjivani Rural Educational Society’s
SANJIVANI COLLEGE OF ENGINEERING
(An Autonomous Institution)
Kopargaon – 423 603, Maharashtra.
Academic Year: Revision : 00
Notes
24-25 Dated : 01.01.2024
Department of Computer Engineering Date of Preparation
Term – II
: 24/01/2025

Prepared By: Prof. S. S. Gawali

You might also like