You are on page 1of 8

2/1/2022

Lecture Outline
SWE1301: Introduction to Problem
Solving and Software Development • Loop Logic Structure
• While loop
• Do-loop

Lecture 09 : Loop Structure


Venue : CIT Theatre
Presented by: M. I. Mukhtar

SWE1301_Introduction to Problem Solving and Software


2
Development

Types of Looping Structure While Loop


• The while loop offers one type of non-fixed
iteration.
For Loop Structure
• Does not need a counter to keep track of the number
of repetitions.
While loop Structure
• While loops are a also pre-test loop, meaning they
check their condition before execution.

Do while loop Structure • While loop is often used for input validation;
• checking input data for errors.

• This lecture will cover while and Do-loop


• While loop body is sometimes never executed

1
2/1/2022

While Loop Algorithm & Flowchart Example 5


Create an algorithm and a flowchart for a password
checking program that does not let a user into an
application until he or she enters the right password.
While (loop condition) 1
The program should display login successful when
Body 2
user enter the correct password.
The while loop executes as Given that : right password is 1234
follow:
Step 1: The loop
expression/condition is
evaluated. If it is TRUE,
the loop body
Step 2: Repeat step (2)
until the loop condition
evaluates to FALSE.

Example 5: Algorithm & Flowchart Coding in Python


password = int(input("Ënter password "))
while(password!=1234):
Step 1 : Enter Password print ("invalid passoword ")
password = int (input("reenter valid password "))
Step 2:
While(password!=1234) print ("login successful")
print “incorrect password”
print “re-enter password”

Step 3 : print “login


successful”

Step 4 : Stop

2/1/2022 SWE1301: Problem Solving and Software Development MIM 8

2
2/1/2022

Example 6 Example 6: Algorithm Solution


Write a program that asks the user to enter an exam Step 1: Enter Marks
mark and display pass if marks is greater than 40.
Otherwise the program should display fail. However Step 2 : While ( Marks < 0 || Marks > 100)
the mark entered should never be greater than 100 or
less than 0. print “ invalid mark”
print “re-enter mark between 1-100”

Step 3 : if (marks > = 40) then:


print “Pass”
else
print “fail”

Step 4: Stop

Example 6: Flowchart Solution Coding in Python


marks = int(input("Enter mark "))
while(marks<0 or marks >100):
print ("invalid mark")
marks = int (input ("re-enter mark between 1-100"))
if(marks > 40):
print ("pass")
else:
print ("fail")

2/1/2022 SWE1301: Problem Solving and Software Development MIM 12

3
2/1/2022

Example 7 Coding in Python


• While loop can also be used for fixed iteration. Write i=1
an algorithm and draw a flowchart to print the first five
integers while(i<6):
print(i)
i= 1 i=i+1
While (i< 6)
print i
i++

Step 1: Assign i = 1
Step 2 : (i <6) repeat
print i
Compute i = i +1
Step 3: End

2/1/2022 SWE1301: Problem Solving and Software Development MIM 14

Do- Loop Algorithm & Flowchart


Do-While Loop
do
• The do…while loop is another variable loop Body ----- 1
construct. While (loop condition) ---- 2

• unlike the while loop, the do…while loop has The while loop executes as
its test at the end of the loop rather than at the follow:
Step 1: The loop body is
beginning.
executed.
Step 2: Then the loop
expression/condition is
• Do-loop body is executed at least once.
evaluated, If loop
condition is TRUE, Repeat
step (1) until the loop
condition evaluates to
FALSE.

4
2/1/2022

Algorithm & Flowchart


Example 8
• Create an algorithm and draw a flowchart that asks
a user to enter the price and tax rate of a product, Do ( )
then calculate its cost using the formula below: Step 1 : Enter price and tax

Cost = price * (tax/100) Step 2 : Cost = price * (tax/100)


a. The algorithm should display the cost of the
product. Step 3 : Display Cost
b. The algorithm should ask the user whether Step 4 : Would you like to
s/he wishes to calculate for another products calculate for another product?
price . ( Use yes or no) reply with Yes or No
c. The algorithm should only terminate when the
user replies with a no.
while (reply == yes) repeat 1-4

Coding in Python Example 9


while True:
• Create an algorithm and draw a flowchart that
price= int(input("enter price ")) asks a user to enter his/her password, select
tax = int(input("enter tax ")) account type, select withdrawal and enter amount.
cost = price * (tax/100) a. The algorithm should display “Please take
print(cost) your cash”.
reply= input("would you like to perform another b. The algorithm should ask the user whether
s/he wishes to perform another transaction . (
transaction, yes or no")
Use yes or no)
if(reply=="no"):
c. The algorithm should only terminate when the
break; user replies with a no.

2/1/2022 SWE1301: Problem Solving and Software Development MIM 19

5
2/1/2022

Algorithm Solution: Example 9 Flowchart Solution: Example 9


do
• Step 1 : Enter password
• Step 2 : Select account type
• Step 3 : Select withdrawal
• Step 4 : Enter amount
• Step 5 : Print “Please take your cash”
• Step 6: Would you like to perform another
transaction? reply with Yes or No
while (reply == “yes”) repeat 1-6

Coding in Python Example 10


while True: • Do loop can also be used for fixed iteration. Write
print ("print enter password") an algorithm and draw a flowchart to print the first
print ("print enter transaction") five integers
print ("print select withdrawal")
print ("print enter amount") i=1
print ("print take cash") Do ( )
• Step 1 : print i
reply = input("do you wish to continue?")
• Step 2 : i ++
if(reply == "no"):
while (i < 6) repeat step 1 - 4
break;

2/1/2022 SWE1301: Problem Solving and Software Development MIM 23

6
2/1/2022

Coding in Python Exercises


i= 1 1. Write an algorithm and draw a flowchart to print
while True: the first 5 even integer numbers.
print(i)
i=i+1 2. Write an algorithm and draw a flowchart to find
if(i>5): the sum and number of numbers between 100 and
200 that are divisible by 7 and prints them.
break;

3. Create an algorithm that uses a loop to compute


the following sum:

SWE1301_Introduction to Problem Solving and Software


2/1/2022 SWE1301: Problem Solving and Software Development MIM 25 26
Development

Exercises Exercises
5. The Kudi Zalla ATM works as follows:
4. You want to write a FOR loop that displays “I love • Entry process: Immediately a user slots a card into the machine,
to program!” 50 times. s/he is prompted to enter a pin. The user stays in the entry process
until a correct pin is entered.
• The Selection process: Once a correct pin is entered the user can
i. What initialization expression will you use? perform any of the three (3) transactions namely withdrawal,
transfer or checking balance. If the user selects the withdrawal
ii. What test expression will you use? option, the ATM displays “Enter the amount required”. Once a valid
iii. What update expression will you use? amount is entered the ATM dispenses the cash and displays “Please
take your cash”. If the user however selects the transfer option, the
iv. Write the loop ATM displays “Enter the destination bank, the destination account
number and the amount to transfer”. It then displays “Transfer
successful” if all the details are correct. If the user, on the other
hand, selects the checking balance option, the ATM displays the
balance in the form “Your balance is XXXXXX”.
• Exit Process: At the end of any of the selected options, the ATM
displays “Do you want to perform another transaction, yes or no?” If
the user replies with a yes, the program will go back to the entry
process; otherwise the user is given back his/her card.
SWE1301_Introduction to Problem Solving and Software SWE1301_Introduction to Problem Solving and Software
27 28
Development Development

7
2/1/2022

Exercises
5. Continues

• Create an algorithm for the entry process. [Given that


Correct pin is 0000].
• Create an algorithm and draw a flowchart for the
selection process. Assume that the entered amount is
always valid and the transfer details are always correct. Questions??
• Create an algorithm for the exit process.

SWE1301_Introduction to Problem Solving and Software


29 2/1/2022 SWE1301: Problem Solving and Software Development MIM 30
Development

You might also like