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

ControlStructures FlowCharts

The document outlines Week 04 of the CS112 Programming Fundamentals course, focusing on imperative programming concepts such as two-way decisions, iteration structures, and the use of for loops in Python. It includes examples of if-else conditions, the structure of loops, and practical coding exercises for students to implement. Additionally, it provides practice problems to reinforce learning on conditional statements and loop execution.

Uploaded by

jawad38289
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 views32 pages

ControlStructures FlowCharts

The document outlines Week 04 of the CS112 Programming Fundamentals course, focusing on imperative programming concepts such as two-way decisions, iteration structures, and the use of for loops in Python. It includes examples of if-else conditions, the structure of loops, and practical coding exercises for students to implement. Additionally, it provides practice problems to reinforce learning on conditional statements and loop execution.

Uploaded by

jawad38289
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

CS112 – PROGRAMMING FUNDAMENTALS

WEEK 04 – IMPERATIVE PROGRAMMING

Course Instructor :
[Link] Khan
Assistant Professor,SE dept
FLOW CHART FOR TWO-WAY PROGRAM

• If the condition temp >


86 is true, the body of
the if statement gets
executed;
• if false, the body of the
else clause gets
executed.
• In both cases, execution
resumes with the
statements after the
if/else pair of
statements.

08/27/2025 2
TWO-WAY DECISIONS

08/27/2025 3
EXECUTION OF TWO-WAY DECISION PROGRAM

When temperature is less than the desired


condition

When temperature is greater than the desired


condition

08/27/2025 4
STRUCTURE OF IF – ELSE CONDITION

 The more general version of the if statement has the following format:

if <condition>:
<indented code block 1>
else:
<indented code block 2>

08/27/2025 5
ITERATION STRUCTURES

 In previous chapter we introduced strings and lists.


 Both are sequences of objects. A string can be viewed as a sequence of one-character
strings; a list is a sequence of objects of any type (strings, numbers, even other lists).
 A task that is common to all sequences is to perform an action on every object in the
sequence.
 For example, you could go down your list of contacts and send a party invite to contacts
living nearby.
 Or you could go through a shopping list to check that you purchased
0 everything
1 2on it.
 Or you could go through the characters of your name in order to spell it.
name F A I S A L animal ‘fish’ ‘cat’ ‘dog’

08/27/2025 6
ANALYSIS OF FOR LOOP EXECUTION

 The for loop statement encompasses lines 4 and 5 of the program. In line 4, char is a
variable name.
 The for loop statement will repeatedly assign characters of string name to variable char.
 If name is string ‘FAISAL', char will first have value ‘F', then ‘A', then ‘I', then ‘S', then ‘A',
and finally ‘L'.
 For each value of char, the indented print statement print(char) is executed.

08/27/2025 7
LOOP IN EXECUTION
 Let’s use this last example. Suppose we would like to implement a short program that spells the
string entered by the user:
Enter a word: FAISAL
The word spelled out:
F
A
I
S
A
L
 The program first requests the user to enter a string. Then, after printing the line 'The word spelled
out:', the characters of the string entered by the user are printed one per line.
08/27/2025 8
CODING FOR SIMPLE LOOP

 We can start the implementation of this program as follows:

name = input('Enter a word: ')


print('The word spelled out:')
 In order to complete this program, we need a method that will allow us to execute a print()
statement for every character of the string name.

08/27/2025 9
EXECUTION OF FOR LOOP

 The Python for loop statement can be used to do exactly this. This program implements
the behavior we want:

Code with iteration using for loop

Executing the code using for loop

08/27/2025 10
ILLUSTRATION OF FOR LOOP OVER STRING

 Figure below illustrates the workings of this loop.


• The variable char is
name F A I S A L
assigned ‘F' in iteration
F 1, ‘A' in iteration 2, ‘I' in
 Iteration 1: char = iteration 3,
A • ‘S' in iteration 4, ‘A' in
 Iteration 2: char =
I iteration 5 , and ‘L' in
 Iteration 3: char = iteration 6; in every
 Iteration 4: char = S iteration, the current
 Iteration 5: A value of char is printed.
char =
• So when char is ‘F', ‘F'
 Iteration 6: char = L gets
printed; when char is
‘A', 08/27/2025 11

‘A' gets printed, and so


FOR LOOP OVER THE LIST
 The for loop can also be used to iterate over the items of a list. In the next example, we
use, in the interactive shell, a for loop to iterate over string objects representing my pets:
>>> animals = ['fish', 'cat', 'dog']
>>> for animal in animals:
print(animal)
fish
cat
dog

08/27/2025 12
ANALYSIS OF FOR- LOOP OVER LIST

 The for loop executes the indented section print(animal) three times, once for each value
of animal; the value of animal is first 'fish', then 'cat', and finally 'dog', as illustrated in
Figure below.
0 1 2

animal ‘fish’ ‘cat’ ‘dog’

‘fish’
 Iteration 1: animal = ‘cat’
 Iteration 2: animal =
‘dog’
 Iteration 3: animal =
08/27/2025 13
THE FOR LOOP VARIABLE !
 The variable char in for x in name:
print(x)
for char in name:
for x in animals:
print(char) print(x)
 and the variable animal in Note: If we change the name of the for loop variable,
we also need to change any occurrence of it in the body
for animal in animals: of the for loop.
print(animal)
 are just variable names, chosen to make the program
more meaningful.
 We could have just as easily written the loops with, say,
variable name x:
08/27/2025 14
FOR LOOP FORMAT

 In general, the for loop statement has this format:

for <variable> in <sequence>:


<indented code block >
<non-indented code block>

08/27/2025 15
NESTING CONTROL FLOW STRUCTURES

 Let’s use the for loop to write a program that combines a for loop and an if statement.
 We would like to write an application that starts by asking the user to enter a phrase.
 After the user has done so, the program will print all the vowels in the phrase, and no other
letter.
 The program should behave like this:

Enter a phrase: test case


e
a
e

08/27/2025 16
STRATEGY FOR NESTING LOOP

 This program will consist of several components.


 We need an input() statement to read in the phrase, a for loop to iterate over the
characters of the input string, and, in every iteration of the for loop, an if statement to
check whether the current character is a vowel.
 If so, it gets printed.
 Next is the complete program.

08/27/2025 17
FOR LOOP WITH CONDITION STATEMENT CODE AND
EXECUTION

Code with for loop and if condition

Executing the code using nesting


loop

08/27/2025 18
ANOTHER SENTENCE FOR FINDING THE VOWELS

• Note that we combined a for loop and an if statement


and that indentation is used to specify the body of each.
• The if statement body is just print(c) while the for loop
statement body is:
if c in 'aeiouAEIOU':
print(c)

08/27/2025 19
FUNCTION RANGE()

 We just saw how the for loop is used to iterate over the items of a list or the characters of a
string. It is often necessary to iterate over a sequence of numbers in a given range, even if
the list of numbers is not explicitly given. For example, we may be searching for a divisor
of a number.
 Or we could be iterating over the indexes 0, 1, 2, . . . of a sequence object. The built-in
function range() can be used together with the for loop to iterate over a sequence of
numbers in a given range. Here is how we can iterate over the integers 0, 1, 2, 3, 4:

08/27/2025 20
FUNCTION RANGE()

08/27/2025 21
FUNCTION RANGE()

 Function range(n) is typically used to iterate over the integer sequence 0, 1, 2, . . . , (n -1).
 In the last example, variable i is set to 0 in the first iteration; in the following iterations, i
gets assigned values 1, 2, 3, and finally 4 (as n = 5).
 As in previous for loop examples, the indented code section of the for loop is executed in
every iteration, for every value of i.

08/27/2025 22
FUNCTION RANGE(START, END)

 The range() function can also be used to iterate over more complex sequences of numbers.
 If we would like the sequence to start at a nonzero number start and end before number
end, we make the function call range(start, end). For example, this for loop iterates over
the sequence 2, 3, 4:

08/27/2025 23
FUNCTION RANGE(START, END, STEP)

 In order to generate sequences that use a step size other than 1, a third argument can be
used.
 The function call range(start, end, step) can be used to iterate over the sequence of integers
starting at start, using a step size of step and ending before end. For example, the next loop
will iterate over the sequence 1, 4, 7, 10, 13:

 The sequence printed by the for loop starts at 1, uses a step size of 3, and ends before 14.
 Therefore it will print 1, 4, 7, 10, and 13.
08/27/2025 24
FUNCTION RANGE(START, END, STEP) PROGRAM EXECUTION

08/27/2025 25
ASSIGNMENT # 3
TO BE SUBMITTED BY SOLVING IT ON PYTHON IDLE & EMAIL IT TO ME. REMEMBER TO READ
CHAPTER 3

08/27/2025 26
PRACTICE PROBLEM 3.1

 Translate these conditional statements into Python if statements:

(a) If age is greater 62, print 'You can get your pension benefits'.
(b) If name is in list ['Musial', 'Aaraon', 'Williams', 'Gehrig', 'Ruth'],
print 'One of the top 5 baseball players, ever!'.
(c) If hits is greater than 10 and shield is 0, print 'You are dead...'.
(d) If at least one of the Boolean variables north, south, east, and west is True, print
'I can escape.'.

08/27/2025 27
PRACTICE PROBLEM 3.3

 Translate these into Python if/else statements:

(a) If year is divisible by 4, print 'Could be a leap year.'; otherwise print 'Definitely not
a leap year.'
(b) If list ticket is equal to list lottery, print 'You won!'; else print 'Better luck next
time...'

08/27/2025 28
PRACTICE PROBLEM 3.4

 Implement a program that starts by asking the user to enter a login id (i.e., a string). The program then
checks whether the id entered by the user is in the list ['joe', 'sue', 'hani', 'sophie'] of valid users.
Depending on the outcome, an appropriate message should be printed. Regardless of the outcome, your
function should print 'Done.' before terminating. Here is an example of a successful login:
Login: joe
You are in!
Done.
 And here is one that is not:

>>>
Login: john
User unknown.
Done.
08/27/2025 29
PRACTICE PROBLEM 3.5

 Implement a program that requests from the user a list of words (i.e., strings) and then
prints on the screen, one per line, all four-letter strings in the list.
>>>
Enter word list: ['stop', 'desktop', 'top', 'post']
stop
post

08/27/2025 30
PRACTICE PROBLEM 3.6

 Write the for loop that will print these sequences of numbers, one per line, in the
interactive shell.
(a) Integers from 0 to 9 (i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
(b) Integers from 0 to 1 (i.e., 0, 1)

08/27/2025 31
PRACTICE PROBLEM 3.7

 Write the for loop that will print the following sequences of numbers, one per line.

(a) Integers from 3 up to and including 12


(b) Integers from 0 up to but not including 9, but with a step of 2 instead of the default
of 1 (i.e., 0, 2, 4, 6, 8)
(c) Integers from 0 up to but not including 24 with a step of 3
(d) Integers from 3 up to but not including 12 with a step of 5

08/27/2025 32

You might also like