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

Conditional Statements

The document outlines three types of control statements in Python: sequential, conditional, and repetition. It details various conditional statements, including if, if...else, if...elif...else, and nested if statements, explaining their syntax and usage for decision-making in code. Additionally, it provides examples of using comparison and logical operators within if statements.

Uploaded by

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

Conditional Statements

The document outlines three types of control statements in Python: sequential, conditional, and repetition. It details various conditional statements, including if, if...else, if...elif...else, and nested if statements, explaining their syntax and usage for decision-making in code. Additionally, it provides examples of using comparison and logical operators within if statements.

Uploaded by

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

Control Statements

There are three types of control statements in python:


1. Sequence or Sequential: The statements execute from top to bottom sequentially
2. Conditional or Selection: Selection used for decisions, branching - choosing between two or more
alternative paths.
3. Repetition or loop or iterative: Repetition used for looping, i.e. repeating a piece of code
multiple times in a row.

Conditional Statements:
Conditional control statements are also called branching or selection statements that
execute one group of instructions depending on the outcome of a decision.

1. if statements
2. if...else statements
3. if...elif...else statements
4. Nested if statements

1
If statements:
The if statement is called a conditional statement. The if keyword performs the basic
conditional test that evaluates a given expression for a Boolean value of True or False.
The condition/expression must be followed by a : colon, then statements to be executed.

The syntax of the if block:

if condition/expression :
statement 1
.....
statement N
if condition is True, then indented statements will be executed

2
if statements using comparison operators:
if a==b :
print ("a is equal to b")
if a<b :
print ("a is less than b")
if a >=b :
print ("a is greater than or equal to b")
if (a != b) :
print ("a and b are not equal")

if statements using logical operators:

Mark1 = int ( input ("Enter Science marks "))


if (Mark1 > 80 and Mark1 < 100):
print ("Grade A")

3
If…else statement:
if…else statement also tests the specified condition/expression. If the condition is True, the commands
in the if part of the block are executed. If the condition is False, the commands under else part will be
executed .
The syntax of the if…else:
if condition/expression :
statement 1
.....
statement N
else:
statement 2
.....
statement N

4
If…elif…else statement:
The if …elif statement allows you to check multiple expressions and executes a block of code
as soon as one of the conditions evaluates to TRUE.

The syntax of the nested if…elif :


if condition/expression :
statement 1
elif condition/expression:
statement 2
else:
statement 3

5
Nested if statements:
One condition can also be nested within another. The nested if…else statement allows you to check for
multiple test expressions and execute different codes for more than two conditions.
The syntax of the nested if construct is:
outer if
if Condition/expression :
statement 1
.....
Inner if
if Condition/expression :
statement 1
.....
else:
statement 1
.....

.....
statement N
else:
statement 1
6
.....

You might also like