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

Chapter 7 - Control Flow Statements

This document discusses control flow statements in Python. It covers conditional statements like if, elif, and else statements which allow altering program flow based on conditions. It also discusses looping statements like for and while loops which allow repeating tasks. The break statement terminates the nearest enclosing loop, while continue skips to the next iteration of the current loop.

Uploaded by

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

Chapter 7 - Control Flow Statements

This document discusses control flow statements in Python. It covers conditional statements like if, elif, and else statements which allow altering program flow based on conditions. It also discusses looping statements like for and while loops which allow repeating tasks. The break statement terminates the nearest enclosing loop, while continue skips to the next iteration of the current loop.

Uploaded by

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

HO CHI MINH CITY UNIVERSITY OF TRANSPORT

FACULTY OF INFORMATION TECHNOLOGY


SOFTWARE ENGINEERING DEPARTMENT

CHAPTER 7
CONTROL FLOW STATEMENTS
CONTENTS

1. Control Flow
2. Control Flow Statements
3. Conditional Statements
4. Looping Statements
5. Break Statement
6. Continue Statement

Introduction to CSE 2
1. Control Flow
• A program’s control flow is the order in which the program’s
code executes.
• All of the programs that we have examined to this point have a
simple flow of control:
• The statements are executed one after the other in exactly the order
given.
→sequential execution.
• What if you wanted to change the flow of how it works?
• For example, you want the program to take some decisions
depending on different situations, such as printing 'Good Morning'
or 'Good Evening' depending on the time of the day?

Introduction to CSE 3
2. Control Flow Statements

• Most programs have a complicated structure.


• Statements may/may not be executed depending on certain
conditions, or groups of statements are executed multiple times.
• Control flow statements control the way the computer executes
programs.
• There are three control flow statements in Python:
• if statement
• for statement
• while statment

Introduction to CSE 4
3. Conditional Statements
• Conditional statements are used to alter the flow of control in the
program.
• They allow for decision making based on condition.
1. if Statement
• Syntax
False (F)
condition
if <condition>:
<statement> True (T)

• <condition>: an expression evaluated. statement


• <statement> is a statement or block of statements
which must be indented.
• There is syntax for branching execution

Introduction to CSE 5
Conditional Statements

Introduction to CSE 6
3. Conditional Statements
• Grouping Statements: Indentation and Blocks
• Syntax
if <condition>: False
<statement>
True <statement>
...
<statement>
<following_statement>

Introduction to CSE 7
Conditional Statements
2. if…else Statement
• Syntax
True (T) False (F)
if <condition>: Condition
<statement_1>
else:
<statement_2>
Statement1 Statement2
• Evaluate a condition and take
one path if it is true but specify
an alternative path if it is not.

Introduction to CSE 8
Conditional Statements

Introduction to CSE 9
Conditional Statements
3. if…elif…else Statement
• There is syntax for branching if <condition_1>:
execution based on several <statement_1>
conditions. elif <condition_2>:
• Use one or more elif clauses. <statement_2>
elif <condition_3>:
• The else clause is optional. If <statement_3>
it is present, there can be only ...
one, and it must be specified else:
last <statement_n>

Introduction to CSE 10
Conditional Statements

Introduction to CSE 11
Conditional Statements
4. Nested if Statement

if <condition_1>:
if <condition_1.1>:
<statement_1.1>
elif <condition_1.2>:
<statement_1.2>
else:
<statement_1.3>
elif <condition_2>:
<statement_2>
...
else:
<statement_n>

Introduction to CSE 12
Conditional Statements

Introduction to CSE 13
Common Errors in Conditional Statements
Note:
• Most common errors in conditional statements are caused by
incorrect indentation.

Introduction to CSE 14
Common Errors in Conditional Statements
Note:
• Which if clause is matched by the else clause?

Introduction to CSE 15
Conditional Statements
5. if…else Statement In One Line
• Syntax

if <condition>: <statement>

• There can even be more than one <statement> on the same line,
separated by semicolons:

if <condition>: <st_1>; <st_2>;…;<st_n>

Introduction to CSE 16
Conditional Statements
6. Conditional expressions
• Python supports one additional decision-making entity called a
conditional expression.
• Syntax

<expr_T> if <conditional_expr> else <expr_F>

• <conditional_expr> is evaluated first. If it is true, the expression


evaluates to <expr_T>. If it is false, the expression evaluates to
<expr_F>.

Introduction to CSE 17
Conditional Statements

Introduction to CSE 18
Conditional Statements
pass Statement
• No limit on the number of statements under the two clauses of an
if statement.But, have to be at least one statement in each block.
• Occasionally, it is useful to have a section with no statements
(empty block).
• a place keeper, scaffolding, for code you haven’t written yet
→ use the pass statement

if <condition>:
pass
else:
pass

Introduction to CSE 19
4. Looping Statements
• Repeating similar tasks without making errors is something that
computers do well and people do poorly.
• Repeated execution of a set of statements is called iteration.
• There are two types of iteration:
• Definite iteration, in which the number of repetitions is specified
explicitly in advance.
• Indefinite iteration, in which the code block executes until some
condition is met.

Introduction to CSE 20
4. Looping Statements
1. for Statement
• Used for definite iteration when you know
exactly how many times the loop body needs to
be executed.
• Syntax: Condition
False (F)

for <var> in <sequence>: True (T)


<statement(s)>
Task A
• <sequence>: holds multiple items of data.
• <var>: a control variable that takes on the values
of the next element in <sequence> each time
through the loop
• <statement(s)>: is loop body, must be indented.
Introduction to CSE 21
4. Looping Statements

Introduction to CSE 22
4. Looping Statements

Introduction to CSE 23
4. Looping Statements
2. while Statement
• Used for definite and indefinite iteration.
• Syntax:

while <condition>:
<statement(s)>

• A while loop executes statements repeatedly as long as a


<condition> remains true.
• <statement(s)>: is loop body, must be indented.

Introduction to CSE 24
4. Looping Statements

Introduction to CSE 25
4. Looping Statements
3. Nested Loops
• Nested loops consist of an while <condition>:
<statement(s)>
outer loop and one or more while <condition>:
inner loops. <statement(s)>
• Each time the outer loop is
repeated, the inner loops are for <var> in <sequence>:
reentered and started anew. <statement(s)>
for <var> in <sequence>:
<statement(s)>

Introduction to CSE 26
4. Looping Statements

Introduction to CSE 27
5. Break Statement
• The break statement is used to
• immediately terminate a loop
• break out of a for or while loop before the loop is finished
• Note:
• In nested loops, break statement terminate the execution of the
nearest enclosing loop in which it appears.

Introduction to CSE 28
6. Continue Statement
• The continue statement
• immediately terminates the current iteration
• Jumps to the next iteration

Introduction to CSE 29

You might also like