CSEN102 Lecture 04 - Notes - 24862
CSEN102 Lecture 04 - Notes - 24862
21.10.2017 - 26.10.2017
1 Synopsis
1.1 Conditional operations
Synopsis – conditional operations
• Rationale
1 if condition:
2 # <operations for the then-part>
3 else
4 # <operations for the else-part>
• Execution
– Evaluate condition expression to see whether it is true or false.
– If true, then execute operations in if-part
– Otherwise, execute operations in else-part
1
Algorithms: operations
Algorithms can be constructed by the following operations:
• Sequential Operation
• Conditional Operation
• Iterative Operation
2 Iterative operations
2.1 Introduction
What is life?
2
2.2 Iterative operation – basics
Iterative Operation – syntax
General Format:
1 while <condition>:
2 step 1: <operation>
3 ...
4 step i: <operation>
Execution
1. Evaluate the condition
2. If condition is true, execute steps 1 to i, then go back to 1.
3. Otherwise, if condition is false continue the execution after the while loop.
Initialize
False
Test Condition
True
Loop Body
succseeding code
count <= 3
2. Formulate the actions for the loop body which take you one step closer to termi-
nation
print("count is:", count)
count = count + 1 # add one to count
3. In general, initialization is required before the loop and some postprocessing
after the loop
count = 1
3
2.4 Iterative operations: Examples
Iterative operations: Example I
Example 1. Given is a natural number n. Compute the sum of numbers from 1 to n.
1 n = eval(input())
2 result = 0
3 i = 1
4 while i <= n:
5 result = (result+i)
6 i = (i+1)
7 print(result)
1 n = eval(input())
2 result = 0
3 i = 1
4 while (i <= n):
5 num = eval(input())
6 result = result + num
7 i = i + 1
8
9 average = result/n
10 print(average)
1 N, M = eval(input()), eval(input())
2 result = 0
3 while M > 0:
4 result = result + N
5 M = M - 1
6 print(result)
4
1 n = eval(input())
2 result = 1
3 while n > 1:
4 result = (result * n)
5 n = (n - 1)
6 print(result)
1 max = -1
2 i = 1
3 while (i <= 4):
4 num = eval(input())
5 if (num > max):
6 max = num
7
8 i = i + 1
9
10 print(max)
True
Increment years
Add interest
to balance
succseeding code
5
Iterative operations: Example VI – Python
Compund interest python:
1 balance = eval(input())
2 rate = eval(input())
3 targetBalance = 20000
4 year = 0
5 while (balance < targetBalance):
6 year = year + 1
7 interest = balance * rate / 100
8 balance = balance + interest
9 print("The investment doubled after")
10 print(year)
11 print("years")
Example 2:
while (x <20): y = y + 1
1 i = 1
2 while i < 10:
3 print(i)
1 A = 1
2 while (A % 2 is 1): # check if A is odd
3 A = A + 2
4 print(A) # the value of A
1 number = eval(input())
2 count = 2
3 result = 0
4 while count < number:
5 result = result + count
6 count = count + 2
6
• Produces incorrect result if number is assigned an even number. Values from
2 to number - 2 will be added (i. e., number is excluded)
1 A, B = eval(input()), eval(input())
2 while not A == B:
3 if A > B:
4 A = A - B
5 else:
6 B = B - A
7 print("The GCD is ")
8 print(A)