SENG 113: Computer Programming I
Doç.Dr. Hilal ARSLAN
Week 9: Loops
Computer Programming
Example
Solution
Example
Solution
Example
Solution
pass
pass
• The pass statement is 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.
Solution
The While-Loop Mechanism
while A Boolean Expression :
The Loop Body
The Boolean expression is checked. If it is true,
then the loop body is executed. The process is
repeated until the Boolean expression is false.
At that point the iteration terminates.
The Broader Context
Code that comes before the loop
while A Boolean Expression :
The Loop Body
Code that comes after the loop
Every variable involved in the Boolean expression must be initialized.
While Loop
• Repeats a statement or group of statements while a given condition is
TRUE.
• It tests the condition before executing the loop body.
• The while loop continues to loop as long as some condition is true.
When the condition becomes false, the looping is discontinued.
Solution
Loops
• while loop is convenient when you don’t have any
idea how many times the loop will be executed
• for loop is usually used in those cases when you
are doing a fixed number of iterations.
Computer Programming
Example
Solution
Example
Loops
k = 0
s = 0 s = 0
while k < 5: for k in range(1,6):
k = k + 1 s = s + k
s = s + k print(k,s)
print(k,s)
Example
k = 0
s = 0 5 15
while k < 5:
k = k + 1
s = s + k
print(k,s)
Only the final value of k and s are reported.
break
Break
• This keyword terminates the loop when a condition is met
Computer Programming
Example
Write a program that plays a simplified number guessing game.
Your program keeps a number (which is always 44). Program asks from
the user a guess number and tell him if it is smaller or bigger than the
kept number. You must keep asking until user guesses your number.
When the user guesses the number correctly, you should tell him/her the
total number of guesses he/she has made.
Computer Programming
Solution
Solution2
Break
Example
Computer Programming
Break
Solution
Computer Programming
Question
• Write a program that reads
positive integer numbers (>=0)
until a negative number is read,
and prints the number of values
entered (except the negative
sentinel value), the smallest
value, and the largest value.
28
Solution
Question
• Write a program which reads
scores of an exam (between 0
and 100) until a negative number
is entered.
It is known that there can be at
most 20 students. Your program
should print the total number of
students actually taken the
exam, and the number students
whose scores are greater than
or equal to the average of the
exam results on separate lines
30
31
32
Generating Fibonacci Numbers
Here are the first 12 Fibonacci Numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
Starting here, each one is the sum
of its two predecessors
• Write a python code generating
Fibonacci numbers
Generating Fibonacci Numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144
Example
Write a python code that reads an integer
number and decides whether this number is
prime or not.
Solution
Example
Solution