Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

NAVODAYA VIDYALAYA SAMITI

HYDERABAD REGION

E-CONTENT
COMPUTER SCIENCE CLASS XI
CONDITIONAL STATEMENTS

IF-ELIF-ELSE
THAMARAI N, PGT CS
JNV PALAKKAD
Learning Objectives

upon this lesson, students would be able to


▪Understand about selection statements.
▪ Understand about how to design flowcharts.
▪ Understand how to do programming using
python selection statements.
FLOW OF CONTROL:
The order of execution of the statements in a
program is known as flow of control. The flow of
control can be implemented using control
structures. Python supports two types of control
structures—selection and repetition.
Python Selection Statements
The selection construct means the execution of statements
depending upon a condition test. If a condition evaluates true,
a course of action (a set of statements) is followed otherwise
another course of action (a different set of statements) is
followed. Python programming language provides following
types of decision making statements.
■ if statements
■ if....else statements
■ if..elif..else statements
■ nested if statements
FLOWCHART

A flowchart is a graphical representation of an algorithm. A flowchart shows


different subtasks with different symbols.Following shows commonly used
flowchart symbols.

PROCESS DECISION

SUBPROCESS START/END

DOCUMENT DATA
EXAMPLE FOR FLOWCHART
IF STATEMENT.
This is the simplest decision-
making statement in Python. It is
used to decide if a particular
PYTHON
block of codeIFneeds
STATEMENTS
to be
executed or not. Whenever the
given condition is True, then the
block of code inside it gets
executed, else it does not
SYNTAX FOR IF

The syntax for if statement:


An if statement should be preceded by the keyword if and ended with a colon :.
The condition of an if statement can be written with or without round brackets.

if condition:

body of if
Example of if statement:

#program to check if num1 is less than num 2


num1, num2 = 5, 6
if(num1 < num2):
print("num1 is less than num2")

Output:num1 is less than num2


Explanation:Here the value 5 is less than 6, so this means the condition is True.
Hence the print statement inside the body of if gets executed.
Shortcut for if statement (Shorthand if or one line if)

If you have only one statement to execute, then you can put it on the same line
as the if statement. Let's try doing this for the above example.
num1, num2 = 5, 6
if(num1 < num2): print("num1 is less than num2")
if else statement

The statements written within the else block get executed whenever the if condition
is False. You can imagine the flow of execution this way,
The syntax for if else statement:

Else statement is preceded by the keyword 'else' and it also ends with a colon :.
Unlike the if, the else statement will not have any conditional statements.
if condition:
body of if
else:

body of else
EXAMPLE

if else in Python Examples:


#program to check if a num1 is less than num2
num1, num2 = 6, 5
if (num1 < num2):
print("num1 is less than num2")
else:
print("num2 is less than num1")
Output:num2 is lesser than num1

Explanation:In the above-given code, the if condition is False and hence the
control shifts to the else block. Hence the statement written within the else block
gets printed.
Shortcut of if else

Shortcut of if else (Shorthand if ... else or One line if


else)

If you have only one statement each for if and else, then they can be
put in the same line. This can be done as shown below
num1, num2 = 6, 5
print("num1 is less than num2") if (num1 < num2) else
print("num2 is less than num1")

Output: num2 is less than num1


elif statement

The keyword elif is a combination of else and if. This statement


works similar to 'else if' statements in the C and other languages. The
statements in elif block get executed when the previous if condition
is False.
The elif statement flowchart:
The syntax for elif statement:
if condition:
body of if
elif condition:
body of elif
else:
body of else
Examples of elif statements:

num1, num2 = 5, 5
if(num1 > num2):
print("num1 is greater than num2")
elif(num1 == num2):
print("num1 is equal to num2")
else:
print("num1 is less than num2")
Output:num1 is equal to num2
Nested if statements

One if statement inside another if statement is called Nested if statement.


The structure and flow of execution can be assumed as shown below.

SYNTAX:
if condition1:
statements
if condition2:
statements
else:
statements
else:
statements
THE FLOW CHART: NESTED IF
Examples of nested if statements:

Let us now try to write a program to check if a given number is


positive or not.
num1 = 5
if (num1 != 0):
if(num1 > 0):
print("num1 is a positive number")
else:
print("num1 is a negative number")
else:
print("num1 is neither positive nor negative")
Output:num1 is a positive number
BIBLIOGRAPHY:

1. Computer Science Textbook for class XI


by NCERT
2. Computer Science with Python
by Sumitha
Arora
3. Computer Science with Python
by Preeti Arora

You might also like