0% found this document useful (0 votes)
28 views24 pages

Control Stuctures

Structured programming comprises logical structures like selection, iteration, and branching. Selection structures evaluate expressions to be true or false and execute different code blocks based on the outcome. Common selection structures include IF, IF-ELSE, and nested IF statements. ELSEIF/ELIF statements allow checking multiple conditions and executing code as soon as one is true.

Uploaded by

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

Control Stuctures

Structured programming comprises logical structures like selection, iteration, and branching. Selection structures evaluate expressions to be true or false and execute different code blocks based on the outcome. Common selection structures include IF, IF-ELSE, and nested IF statements. ELSEIF/ELIF statements allow checking multiple conditions and executing code as soon as one is true.

Uploaded by

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

SELECTION ITERATION

TYPES OF 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
Decision Making / selection
 Decision Making
• Decision structures evaluate multiple expressions which
produce TRUE or FALSE as outcome.
• Needs to determine which action to take and which
statements to execute if outcome is TRUE or FALSE
otherwise.
• TRUE
• any non-zero and non-null values 
• FALSE
• either zero or null
Typical decision making structure
 Decision Making
 Flow graph

Condition

If the condition is If the condition is


true FALSE

Conditional
Code
Types of decision making statements.
 Decision Making
 Flow graph
 Types of Decision
making systems
Python IF Statement
 Decision Making • The if statement contains a logical expression using which
 Flow graph data is compared and a decision is made based on the
 Types of Decision result of the comparison.
making systems
 IF Statement
• Syntax
 Syntax if expression:
statement(s)
• Flow Diagram
Flow Diagram
 Decision Making
 Flow graph
 Types of Decision
If the condition is
making systems true
 IF Statement Condition
 Syntax
 Flow graph

If the condition is
FALSE
Conditional
Code
Example
 Decision Making var1 = 100
 Flow graph
if var1:
 Types of Decision
making systems print ("1 - Got a true expression value")
 IF Statement print (var1)
 Syntax
 Flow graph
 Example
var2 = 0
if var2:
print ("2 - Got a true expression value")
print (var2)
print ("Good bye!")
Python IF... ELSE Statements
 Decision Making • An else statement can be combined with an if statement.
 Flow graph
• An else statement contains the block of code that executes
 Types of Decision
making systems
if the conditional expression in the if statement resolves to
 IF Statement
0 or a FALSE value.
 Syntax • Syntax
 Flow graph
 IF-ELSE statement
if expression:
 Syntax statement(s)
else:
statement(s)
Flow Diagram
 Decision Making
 Flow graph
 Types of Decision
If the condition is
making systems true
 IF Statement Condition
 Syntax
 Flow graph
If the condition is
 IF-ELSE statement FALSE
 Syntax
 Flow graph
FALSE Conditional TRUE Conditional
Code Code
Example
 Decision Making var1 = 100
 Flow graph
if var1:
 Types of Decision
making systems print ("1 - Got a true expression value")
 IF Statement print (var1)
 Syntax
 Flow graph else:
 IF-ELSE statement print ("1 - Got a false expression value")
 Syntax
 Flow graph print (var1)
 Example
Example
 Decision Making var2 = 0
 Flow graph
if var2:
 Types of Decision
making systems print ("2 - Got a true expression value")
 IF Statement print (var2)
 Syntax
 Flow graph else:
 IF-ELSE statement print ("2 - Got a false expression value")
 Syntax
 Flow graph print (var2)
 Example

print ("Good bye!")


The elif Statement
 Decision Making • The elif statement allows to check multiple expressions for
 Flow graph TRUE and
 Types of Decision
making systems
• execute a block of code as soon as one of the conditions
 IF Statement
evaluates to TRUE.
 Syntax • Similar to the else, the elif statement is optional.
 Flow graph
 IF-ELSE statement
• However, unlike else, for which there can be at most one
 Syntax statement,
 Flow graph • there can be an arbitrary number of elif statements
 Example
 Elif Statement
following an if.
Syntax: The elif Statement
 Decision Making if expression1:
 Flow graph
statement(s)
 Types of Decision
making systems elif expression2:
 IF Statement statement(s)
 Syntax
 Flow graph elif expression3:
 IF-ELSE statement statement(s)
 Syntax
 Flow graph else:
 Example
 Elif Statement
statement(s)
• Syntax
Syntax: The elif Statement
 Decision Making var = 100
 Flow graph
if var == 200:
 Types of Decision
making systems print ("1 - Got a true expression value")
 IF Statement print (var)
 Syntax
 Flow graph elif var == 150:
 IF-ELSE statement print ("2 - Got a true expression value")
 Syntax
 Flow graph print (var)
 Example
 Elif Statement
• Syntax
• Example
Syntax: The elif Statement
 Decision Making elif var == 100:
 Flow graph
print ("3 - Got a true expression value")
 Types of Decision
making systems print (var)
 IF Statement else:
 Syntax
 Flow graph print ("4 - Got a false expression value")
 IF-ELSE statement print (var)
 Syntax
 Flow graph
 Example
 Elif Statement
print ("Good bye!")
• Syntax
• Example
Example
 Decision Making mark= int(input("Enter the marks out of 100="))
 Flow graph if mark>=90:
 Types of Decision print("Attained grade is 'S'")
making systems
 IF Statement
elif mark>=80:
 Syntax print("Attained grade is 'A'")
 Flow graph elif mark>=70:
 IF-ELSE statement
print("Attained grade is 'B'")
 Syntax
 Flow graph elif mark>=60:
 Example print("Attained grade is 'C'")
 Elif Statement
• Syntax elif mark>=50:
• Example
print("Attained grade is 'D'")
else:
print("Attained grade is 'Fail'")
Nested IF statements
 Decision Making • There may be a situation when you want to check for
 Flow graph another condition after a condition resolves to true.
 Types of Decision
making systems
• In such a situation, you can use the nested ifconstruct.
 IF Statement • In a nested if construct, you can have
 Syntax an if...elif...else construct inside
 Flow graph
another if...elif...else construct.
 IF-ELSE statement
 Syntax
 Flow graph
 Example
 Elif Statement
• Syntax
• Example
 Nested IF Staements
Syntax: Nested IF statements
 Decision Making if expression1:
 Flow graph statement(s)
 Types of Decision if expression2:
making systems
 IF Statement statement(s)
 Syntax elif expression3:
 Flow graph
statement(s)
 IF-ELSE statement
 Syntax else:
 Flow graph statement(s)
 Example
 Elif Statement elif expression4:
• Syntax statement(s)
• Example
 Nested IF Statements
else:
 Syntax statement(s)
Example
 Decision Making var = 100
 Flow graph if var < 200:
 Types of Decision print ("Expression value is less than 200")
making systems if var == 150:
 IF Statement print ("Which is 150")
 Syntax
elif var == 100:
 Flow graph
print ("Which is 100")
 IF-ELSE statement
elif var == 50:
 Syntax
 Flow graph print ("Which is 50")
 Example elif var < 50:
 Elif Statement print ("Expression value is less than 50")
• Syntax
• Example else:
 Nested IF Statements print ("Could not find true expression")
 Syntax
 Example print ("Good bye!")
Example
 Decision Making
 Flow graph
 Types of Decision
making systems
 IF Statement
 Syntax
 Flow graph
 IF-ELSE statement
 Syntax
 Flow graph
 Example
 Elif Statement
• Syntax
• Example
 Nested IF Statements
 Syntax
 Example
Example
 Decision Making • Multiple conditions can be check in a ‘if’ statement using
 Flow graph logical operators ‘and’ and ‘or’.
 Types of Decision
making systems
• Python code to print ‘excellent’ if mark1 and mark2 is
 IF Statement greater than or equal to 90, print ‘good’ if mark1 or
 Syntax mark2 is greater than or equal to 90, print ‘need to
 Flow graph improve’ if both mark1 and mark2 are lesser than 90
 IF-ELSE statement
 Syntax
 Flow graph
 Example
 Elif Statement
• Syntax
• Example
 Nested IF Statements
 Syntax
 Example
Python Code
 Decision Making mark1=int(input("mark1="))
 Flow graph
mark2=int(input("mark2="))
 Types of Decision
making systems if(mark1>=90 and mark2>=90):
 IF Statement
print("Excellent")
 Syntax
 Flow graph elif(mark1>=90 or mark2>=90):
 IF-ELSE statement

print("Good")
Syntax
 Flow graph else:
 Example
 Elif Statement print("Needs to improve")
• Syntax
• Example
 Nested IF Statements
 Syntax
 Example
Summary

You might also like