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

Lecture 4 - Control Flow

Uploaded by

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

Lecture 4 - Control Flow

Uploaded by

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

University of Juba

School of Computer Science & IT

Course Unit: CCS 218


Programming Fundamentals II
Mr. David L. Aggrey (MSc. In Computer Science)
Course Unit: Data Structures and Algorithms
Course Outlines
› Chapter 1: Getting Started with Python
› Chapter 2: Variables and Data Types
› Chapter 3: Operators and Expressions
› Chapter 4: Control Flow
› Chapter 5: Functions
› Chapter 6: Data Structures
› Chapter 7: Regular Expressions
› Chapter 8: File Handling
› Chapter 9: Modules and Packages
› Chapter 10: Exception Handling
CCS 218 – Programming Fundamentals II Mr. David L. A. Abbas
University of Juba
School of Computer Science & IT

Chapter 4
Control Flow
CCS 218: Programming Fundamentals II
Chapter One: Getting Started with Python
Decision Control Statement
› Program execution is sequential and one way to alter the course of
execution is to use DECISION MAKING STATEMENTS
› Decision can be made on the basis of SUCCESS or FAILURE of some
logical condition (which are included in the program as Decision
Control Statement)
› Python supports the following decision making control statements:
• The if statement
• The if-else statement
• The Nested if-else statement
• The elif ladder statement
• The match –case statement.
CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas
Chapter One: Getting Started with Python
The if statement
› The if statement is used to execute/skip a block of statements on
the basis of truth or falsity of a condition.
› The condition to be checked is put inside the parenthesis which is
preceded by keyword if

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The if statement Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The if-else statement
› The if-else statement is used to execute a block of statements on
the basis of truth of a condition OR another block of statement
based on falsity of a condition.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Nested if-else
› Nesting of if-else means having one if-else or simple if as the part of
another if-else or simple if statement. There may be various
syntaxes of nesting of if-else we present few of them:

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Nested if-else’s - Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The elif Ladder
› With the elif ladder, if the any if condition is satisfied, then all its
related statements are executed and all other elif’s are skipped.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The elif Ladder

› EXERCISE: Modify the above python code to display the minimum value
CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas
Chapter One: Getting Started with Python
Match Case Selection Structure
› Switch-case statement can be used to replace else-if ladder
construct. Its general syntax is as follows:

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Match Case Selection Structure
› The match statement is a multi-way branch statement introduced in
python 3.10.
› Useful when there is a possibility to make a choice from a number of
options
› How does it work?
› Match and case are reversed keywords.
› The C language break statement used in switch() to passed control
outside the switch() block is not need in python.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Match Case Selection Structure - Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Match Case Selection Structure - Example

Iteration Control
(Looping)

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Introduction to Looping
› Looping is a process in which set of statements are executed
repeatedly for a finite or infinite number of times.
› In our practical life we see lots of examples where some
repetitive tasks has to be performed like finding average
marks of students of a class, finding maximum salary of
group of employees, counting numbers etc.

› A loop is a block of statements with which are executed


again and again till a specific condition is satisfied.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
Introduction to Looping
› Python provides three loops to perform repetitive action.
› while
› for
› do-while
› To work with any types of loop three things have to be
performed :
› Loop control variable and its initialization.
› Condition for controlling the loop.
› Increment / decrement of control variable.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The while Loop
› The syntax of the while loop is:

› The indented statements arecalled body of the while loop.


› All the statements within the body are repeated till the
condition specified in the parenthesis in while is satisfied.
› As soon as condition becomes false the body is skipped and
control is transferred to the next statement outside the loop.
CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas
Chapter One: Getting Started with Python
The while Loop - Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The while Loop - Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The Break statement
› The Break statement is used to come out early from loop
without waiting for the condition to become false.
› When the break statement is encountered in the while loop
or any of the loops, the control immediately transfers to first
statement out of the loop i.e., loop is exited prematurely.
› If there is nesting of loops the break will exit only from the
current loop containing it.

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The Continue statement
› The continue statement causes the remainder of the
statements following the continue to be skipped and
continue with the next iteration of the loop.
› We use continue statement to bypass certain number of
statements in the loop on the basis of some condition given
by if generally.
› The syntax of continue statement is simply
continue;

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The Break Statement - Example

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The for Loop
› In Python, the for loop is used to iterate over a sequence (such as a list,
tuple, string, or range). The basic syntax of a for loop is as follows:
for variable in sequence:
# Code block to be executed for each element in the sequence
̵ Using range() to Iterate Over a Sequence of Numbers:
for i in range(5): # range(5) generates numbers from 0 to 4
print(i)
̵ Using range() with Start and End:
for i in range(2, 7): # range(2, 7) generates numbers from
# 2 to 6
print(i)
̵ Using range() with Start, End, and Step:
for i in range(1, 10, 2): # range(1, 10, 2) generates
# numbers 1, 3, 5, 7, 9
print(i)

CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas


Chapter One: Getting Started with Python
The Break Statement - Example

To be continued
CBE 312 - Data Structures and Algorithms Mr. David L. A. Abbas

You might also like