SlideShare a Scribd company logo
Control
Structure
Conditional Statements (Decision Making)
 Python language provide the following conditional (Decision making) statements.
If statements
Else statements
Elif statements
Nested if
statements
If Statement
 In Python, If Statement is used for decision making.
 It will run the body of code only when IF statement is true.
 When you want to justify one condition while the other condition is not true, then
you
use "if statement".
Syntax:
Let see an example-
What happen when "if condition" does not
meet
 In this step, we will see what happens when your "if condition" does not
meet.
 Code Line 5: We define two variables x, y = 8, 4
 Code Line 7: The if Statement checks for condition x<y which is False in
this case
 Code Line 8: The variable st is NOT set to "x is less than y."
 Code Line 9: The line print st - is trying to print the value of a variable that was
never declared. Hence, we get an error.
How to use "else statement"
 The "else statement" is usually used when you have to judge one statement on the
basis of other.
 If one condition goes wrong, then there should be another condition that should
justify the statement or logic.
 Code Line 5: We define two variables x, y = 8, 4
 Code Line 7: The if Statement checks for condition x<y which is False in this case
 Code Line 9: The flow of program control goes to else condition
 Code Line 10: The variable st is set to "x is greater than y."
 Code Line 11: The line print st will output the value of variable st which is "x is greater
than y"
When "else condition" does not work
 There might be many instances when your "else condition" won't give you the desired result.
 It will print out the wrong result as there is a mistake in program logic.
 In most cases, this happens when you have to justify more than two statement or condition in a
program.
How to use "elif" statement
 To correct the previous error made by "else condition", we can
use "elif" statement.
 By using "elif" condition, you are telling the program to print
out the third
condition or possibility when the other condition goes wrong
or incorrect.
 Code Line 5: We define two variables x, y = 8, 8
 Code Line 7: The if Statement checks for condition x<y which is False in this case
 Code Line 10: The flow of program control goes to the elseif condition. It checks
whether x==y which is true
 Code Line 11: The variable st is set to "x is same as y."
 Code Line 15: The flow of program control exits the if Statement (it will not
get to the else Statement).
 And print the variable st. The output is "x is same as y" which is correct
How to execute conditional statement with
minimal code
Syntax:
A If B else C
Example:
 Code Line 2: We define two variables x, y = 10, 8
 Code Line 3: Variable st is set to "x is less than y "if x<y or
else it is set to "x is greater than or equal to y".
 In this x>y variable st is set to "x is greater than or equal
to y."
 Code Line 4: Prints the value of st and gives the correct output
Nested If Statement
When there is an if statement (or if..else or if..elif..else) is present inside another if
statement (or if..else or if..elif..else) then this is calling the nesting of control
statements.
Here we have a if statement inside another if..else statement block. Nesting control
statements makes us to check multiple conditions.
num = -99
if num >
0
pri
nt(
“po
siti
ve
nu
mb
Output:
negative number
two digit negative number

More Related Content

PPTX
python conditional statement.pptx
PPTX
conditional statement in python day 5.pptx
PPTX
Introduction-to-Conditional-Statements-in-Python.pptx
PPTX
Python decision making part4
PPT
Conditional Statement.ppt
PPTX
Conditionalstatement
PDF
if else.pdf
PPTX
PRESENTATION.pptx
python conditional statement.pptx
conditional statement in python day 5.pptx
Introduction-to-Conditional-Statements-in-Python.pptx
Python decision making part4
Conditional Statement.ppt
Conditionalstatement
if else.pdf
PRESENTATION.pptx

Similar to control-structure.pptxcontrol-structure.pptx (20)

PDF
loops.pdf
PDF
W4-DecisionMaking.pdf. python document. CS
PPTX
1.4 decision control statements IN PYTHON.pptx
PDF
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
PDF
Conditional-Statement.pdf
PPTX
Conditional and control statement
PDF
Python decision making
PPTX
Conditional Statements.pptx
PPTX
Baabtra.com little coder chapter - 5
PPTX
FLOW OF CONTROL-INTRO PYTHON
PDF
flow of control python.pdf
PDF
flow of control python.pdf
PPT
Mesics lecture 6 control statement = if -else if__else
PPTX
2-Conditional Statements, decision making (3).pptx
PPTX
Conditionals
PPTX
Decision Structures
PPTX
New Microsoft PowerPoint Presentation.pptx
DOCX
conditional statements.docx
PPTX
Programming note C#
PPTX
Control_Statements.pptx
loops.pdf
W4-DecisionMaking.pdf. python document. CS
1.4 decision control statements IN PYTHON.pptx
conditionalanddvfvdfvdvdcontrolstatement-171023101126.pdf
Conditional-Statement.pdf
Conditional and control statement
Python decision making
Conditional Statements.pptx
Baabtra.com little coder chapter - 5
FLOW OF CONTROL-INTRO PYTHON
flow of control python.pdf
flow of control python.pdf
Mesics lecture 6 control statement = if -else if__else
2-Conditional Statements, decision making (3).pptx
Conditionals
Decision Structures
New Microsoft PowerPoint Presentation.pptx
conditional statements.docx
Programming note C#
Control_Statements.pptx
Ad

More from usha raj (20)

PPTX
New Microsoft PowerPoint Presentation.pptx
PPTX
BOM 28.06.2024.pptxBOM 28.06.2024.pptxBOM 28.06.2024.pptx
PPTX
INTROTOPROBLEMSOLVING.pptxINTROTOPROBLEMSOLVING.pptx
PPT
Chapter 2 Data Representation.pptChapter 2 Data Representation.ppt
PPTX
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
PPTX
CSPPT_class12.pptxclass11_CS_control statements7
PPTX
memory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptx
PPTX
ComputerNEtWorksComplete.pptxComputerNEtWorksComplete.pptx
PPTX
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
PPTX
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
PPTX
7-2-data-structures-ii-stacks-queues.pptx
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPTX
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
PPTX
APJ ABDUL KALAM (final draft).pptx sampl
PPTX
BITCOIN PRICE PREDICTION USING MACHINE LEARNING.pptx
PPTX
Cyber crimes and cyber security.pptx filetx
PPTX
ResultAnalysis_PT3_Sahodaya_24_25_Class1_to_12.pptx
PPTX
ResultAnalysis_PreBoard_Sahodaya24-25.pptx
PPTX
python db connection samples and program
PPTX
health and education presentation demonstration
New Microsoft PowerPoint Presentation.pptx
BOM 28.06.2024.pptxBOM 28.06.2024.pptxBOM 28.06.2024.pptx
INTROTOPROBLEMSOLVING.pptxINTROTOPROBLEMSOLVING.pptx
Chapter 2 Data Representation.pptChapter 2 Data Representation.ppt
sql12.pptxsql12.pptxsql12.pptxsql12.pptx
CSPPT_class12.pptxclass11_CS_control statements7
memory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptxmemory -team 2.pptx
ComputerNEtWorksComplete.pptxComputerNEtWorksComplete.pptx
List_tuple_dictionary.pptxList_tuple_dictionary.pptx
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
7-2-data-structures-ii-stacks-queues.pptx
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
2_3 Functions 5d.pptx2_3 Functions 5d.pptx
APJ ABDUL KALAM (final draft).pptx sampl
BITCOIN PRICE PREDICTION USING MACHINE LEARNING.pptx
Cyber crimes and cyber security.pptx filetx
ResultAnalysis_PT3_Sahodaya_24_25_Class1_to_12.pptx
ResultAnalysis_PreBoard_Sahodaya24-25.pptx
python db connection samples and program
health and education presentation demonstration
Ad

Recently uploaded (20)

PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Cell Types and Its function , kingdom of life
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Lesson notes of climatology university.
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Computing-Curriculum for Schools in Ghana
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Cell Types and Its function , kingdom of life
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Anesthesia in Laparoscopic Surgery in India
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Weekly quiz Compilation Jan -July 25.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
History, Philosophy and sociology of education (1).pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Microbial disease of the cardiovascular and lymphatic systems
Lesson notes of climatology university.
Yogi Goddess Pres Conference Studio Updates
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Computing-Curriculum for Schools in Ghana
Chinmaya Tiranga quiz Grand Finale.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer

control-structure.pptxcontrol-structure.pptx

  • 2. Conditional Statements (Decision Making)  Python language provide the following conditional (Decision making) statements. If statements Else statements Elif statements Nested if statements
  • 3. If Statement  In Python, If Statement is used for decision making.  It will run the body of code only when IF statement is true.  When you want to justify one condition while the other condition is not true, then you use "if statement". Syntax:
  • 4. Let see an example-
  • 5. What happen when "if condition" does not meet  In this step, we will see what happens when your "if condition" does not meet.
  • 6.  Code Line 5: We define two variables x, y = 8, 4  Code Line 7: The if Statement checks for condition x<y which is False in this case  Code Line 8: The variable st is NOT set to "x is less than y."  Code Line 9: The line print st - is trying to print the value of a variable that was never declared. Hence, we get an error.
  • 7. How to use "else statement"  The "else statement" is usually used when you have to judge one statement on the basis of other.  If one condition goes wrong, then there should be another condition that should justify the statement or logic.
  • 8.  Code Line 5: We define two variables x, y = 8, 4  Code Line 7: The if Statement checks for condition x<y which is False in this case  Code Line 9: The flow of program control goes to else condition  Code Line 10: The variable st is set to "x is greater than y."  Code Line 11: The line print st will output the value of variable st which is "x is greater than y"
  • 9. When "else condition" does not work  There might be many instances when your "else condition" won't give you the desired result.  It will print out the wrong result as there is a mistake in program logic.  In most cases, this happens when you have to justify more than two statement or condition in a program.
  • 10. How to use "elif" statement  To correct the previous error made by "else condition", we can use "elif" statement.  By using "elif" condition, you are telling the program to print out the third condition or possibility when the other condition goes wrong or incorrect.
  • 11.  Code Line 5: We define two variables x, y = 8, 8  Code Line 7: The if Statement checks for condition x<y which is False in this case  Code Line 10: The flow of program control goes to the elseif condition. It checks whether x==y which is true  Code Line 11: The variable st is set to "x is same as y."  Code Line 15: The flow of program control exits the if Statement (it will not get to the else Statement).  And print the variable st. The output is "x is same as y" which is correct
  • 12. How to execute conditional statement with minimal code Syntax: A If B else C Example:
  • 13.  Code Line 2: We define two variables x, y = 10, 8  Code Line 3: Variable st is set to "x is less than y "if x<y or else it is set to "x is greater than or equal to y".  In this x>y variable st is set to "x is greater than or equal to y."  Code Line 4: Prints the value of st and gives the correct output
  • 14. Nested If Statement When there is an if statement (or if..else or if..elif..else) is present inside another if statement (or if..else or if..elif..else) then this is calling the nesting of control statements. Here we have a if statement inside another if..else statement block. Nesting control statements makes us to check multiple conditions. num = -99 if num > 0 pri nt( “po siti ve nu mb