[go: up one dir, main page]

0% found this document useful (0 votes)
199 views8 pages

Unit 2 Basic Python: Control Statements

The document discusses three main control structures in Python: sequential, selection, and repetition. It defines each structure and provides examples. The sequential structure runs code in order. The selection structures (if, if/else, if/elif/else) allow running different code blocks depending on conditions. The repetition structures (while, for) allow running code blocks repeatedly as long as a condition is true. Flowcharts and pseudocode are given to illustrate examples of each control structure.

Uploaded by

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

Unit 2 Basic Python: Control Statements

The document discusses three main control structures in Python: sequential, selection, and repetition. It defines each structure and provides examples. The sequential structure runs code in order. The selection structures (if, if/else, if/elif/else) allow running different code blocks depending on conditions. The repetition structures (while, for) allow running code blocks repeatedly as long as a condition is true. Flowcharts and pseudocode are given to illustrate examples of each control structure.

Uploaded by

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

Unit 2 Basic Python : Control Statements

control structures

• 3 control structures
• Sequential structure
• Built into Python
• Selection structure
• The if statement
• The if/else statement
• The if/elif/else statement
• Repetition structure
• The while repetition structure
• The for repetition structure
Sequence Control Structure

add grade to total = total + grade;


total

add 1 to counter counter = counter + 1;

Fig. 3.1 Sequence structure flowchart with pseudo code.

3
if Selection Structure

true
Grade >= 60 print “Passed”

false

Fig. 3.3 if single-selection structure


flowchart.
4
if/else Structure

false true
Grade >= 60

print “Failed” print “Passed”

Fig. 3.4 if/else double-selection structure flowchart.

5
if/elif/else Selection Structure
true
if statement condition a case a action(s)
false

true
first elif condition b case b action(s)
statement false

.
.
.

last true
condition z case z action(s)
elif
false
statemen
t else default action(s)

stateme
nt
Fig. 3.5 if/elif/else multiple-selection structure.

6
while Repetition Structure
• Repetition Structures
• Allow a program to repeat an action while a condition is true
• Using while Repetition
• Action(s) contained within the body of the loop
• Condition should evaluate to false at some point
• Otherwise infinite loop and program hangs

7
while Repetition Structure

true
Product <= 1000 Product = 2 *
product
false

Fig. 3.8 while repetition structure flowchart.

You might also like