0% found this document useful (0 votes)
13 views20 pages

COM 400lesson 2

The document discusses control flow in programming, specifically in Python, detailing control structures that direct execution flow, including decision, looping, and jumping statements. It explains various conditional statements (if, if-else, if-elif-else), looping structures (for and while loops), and transfer control statements (break, continue, pass). Examples and syntax for each type of control statement are provided to illustrate their usage in programming.

Uploaded by

kenbrian326
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)
13 views20 pages

COM 400lesson 2

The document discusses control flow in programming, specifically in Python, detailing control structures that direct execution flow, including decision, looping, and jumping statements. It explains various conditional statements (if, if-else, if-elif-else), looping structures (for and while loops), and transfer control statements (break, continue, pass). Examples and syntax for each type of control statement are provided to illustrate their usage in programming.

Uploaded by

kenbrian326
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/ 20

CONTROL FLOW

By Samwel Tarus
Control Structures / Statements
Directs the flow of execution in a program.
Control structures enable the programmer to achieve iteration and branching.

Python language control statement are divided into three categories:

1. Decision/Conditional control structures/statements


2. Looping/Iteration control structures
3. Jumping/Transfer control structures
Branching: The process of taking the alternative course of action against the
other.
Looping: The process of repeating / iterating a block of statements any number
of times.
Jumping: Moving to a specified location / point in a program
Introduction
• Flow control describes the order in which statements will be executed at
runtime.
Conditional Statements
If control statement
if condition: statement
or
if condition:
statement-1
statement-2
statement-3
statement -4
 flowIf.py
If condition is true then statements will be executed. evenNo.py
If else control statement
Syntax:
if condition:
Action-1
else:
Action-2

 If condition is true Action-1 will be executed otherwise Axtion-2 will be executed.


flowIfElse.py
Cont’d …
if-elif-else:
 Syntax:
if condition1:
Action-1
elif condition-2:
Action-2
elif condition-3:
Action-3
else
Default Action
ifElifElse.py
 Program to read name and marks of the student and to display the corresponding
grade using if-elif-else structure. marksheet1.py
 Program to find biggest of given 3 input integer numbers:
biggest3.py, marksheet.py
Looping/Iterative Control structures/statements
Facilitate the action of repetition / iteration

Examples:

i. for loop
ii. while loop
For Loop
 Designed to repeatedly execute a code block while iterating through a list, tuple,
dictionary, or other iterable objects of Python.
 Python supports 2-types of iterative statement (Definite and Indefinite iteration)
 Syntax:
for value in sequence: #indefinite iteration
{ code block }
 The variable value is used to hold the value of every item present in the
sequence before the iteration begins until this particular iteration is completed.
 Loop iterates until the final item of the sequence are reached.
Example:
# Creating a sequence which is a tuple of numbers
numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2] demoDemo.py
 To print Hello 10 times. #definite iteration
for x in range(10):
print("Hello")

 To display numbers from 0-10


for x in range(10):
print(x)
 To display odd numbers from 0-20
for x in range(21):
if(x%2 !=0 ):
print(x)
 To display numbers from 10-1 in descending order.
for x in range(10,0,-1):
print(x)
For loop in List, Tuple, Dictionary etc
 Create a list and display items in the list: fruitList.py

 Create a tuple and display items in the tuple. forTuple.py

 Create a string and traverse through the string displaying characters one by one.
forString.py
 Create a set then display items in the set. forSet.py

 Create and display items in a dictionary. forDict.py,


While loop
While loop
 Entry loop control structure
 Syntax:
while condition:
body

 To print numbers from 1 to 10 by using while loop: whileLoop1.py


 To print even numbers and compute its sum. whileLoop2.py
 To display Fibonacci series to nth term. Fibbonacci.py
Transfer control statements
1. break control structure / statement

2. continue control structure / statement


break control structure / statement
Transfers control of execution outside the switch control structure
Transfers control of execution from a deeply nested loop.
Transfers control of execution in the forward direction.
Syntax: break;

T
Cond break

Normal F
Loop
Program examples break

 Program to display numbers. break1.py

 Program to count the first 5 values in a tuple. break2.py

 Program to compute sum using while loop and break statement. break3.py
Continue control structure

1. Transfers control to the beginning


of the loop or program

2. Transfers control in the backward


direction

3. By passes the remainder of the


loop.

Syntax: continue
Program Examples Continue
 Program to demonstrate continue: continue2.py
 Program to display odd numbers between 1 and 10. continue3.py
 To compute the sum of positive integer numbers from a set of 5 input integer
numbers. continue1.py
Pass statement
 Statement used as a placeholder for future code.
 When the pass statement is executed, nothing happens, but you avoid getting an
error when empty code is not allowed.
 Empty code is not allowed in loops, function definitions, class definitions, or in if
statements.
 Example:
#Create a placeholder for future code:
for x in [0, 1, 2]:
pass
 passDemo.py

You might also like