You are on page 1of 16

FAKULTI TEKNOLOGI KEJURUTERAAN

ELEKTRIK DAN ELEKTRONIK


UNIVERSITI TEKNIKAL MALAYSIA MELAKA

TECHNOLOGY SYSTEM PROGRAMMING 2

BEEL 1234 SEMESTER 2 SESI 2020/2021

LAB 3: REPETITION CONTROL TECHNIQUES

NO. STUDENTS' NAME MATRIC. NO.

1.

2.

3.

PROGRAMME 1BEEL

SECTION /
1/1 1/2
GROUP

1. AMAR FAIZ BIN ZAINAL ABIDIN


NAME OF
INSTRUCTOR(S)
2.

EXAMINER’S COMMENT(S) TOTAL MARKS


1.0 OBJECTIVES

1. To familiarize with Python programming control structure.


2. To create a program using a while statement.
3. To create a program using a for statement.
4. To translate C code to the Python code.
5. To work effectively in a given task in the group.

2.0 EQUIPMENT

1. Personal Computer / desktop


2. Software: Visual Studio Code

3.0 SYNOPSIS& THEORY

3.1 Iteration Essentials


Most programs involve iteration, or looping. A loop is a group of instructions the
computer executes repeatedly while some loop-continuation condition remains true.
There are 2 types of iteration:
a) Counter-controlled iteration
Sometimes called difinite iteration because we know in advanced exactly how
man times the loop will be executed. A control variable i used to count the
number of iterations. The control variable is incremented (usually by 1) each
time the group of instructions is performed. When the value of the control
variable indicates the correct number of iterations has been performed, the loop
terminates and execution continues with the statement after the iteration
statement.

b) Sentinel-controlled iteration
Sometimes called indifinite iteration because it’s not known in advance how
many times the loop will be executed. Sentinel values are used to control the
iteration when:
i. The precise number of iterations isn't known in advance.
ii. The loop includes statements that obtain data each time the loop is
performed.
The sentinel indicates “end of data”, which is entered after all regular data items
have been supplied to the program. Sentinel must be distinct from regular items.

1
3.1.1 The while iteration statement
Consider a program segement below, to find the first power of 3 larger than 100.
The integer variable product has been initialised to 3. When the following code
finishes executing, will contain the desired answer.

product = 3
while product <= 100:
product = 3 * product

The flowchart of Figure 3.1 illustrates the flow control reflecting the while iteration
statement above. The flowchart clearly shows the iteration. The flowline emerging from
teh rectangle wraps back to the decision, which is tested each time through the loop
until the decision eventually becomes false. At this point, the while statement is exited
and control passes to the next statement in the program.

true
product <= 100 product = 3*product

false

Figure 3.1: Flowcharting the while iteration statement.

When the while statement is entered, the value of product is 3. The variable
product is repeatedly mutiply by 3, taking the value of 9, 27, and 81 successively.
When product becomes 243, the condition in the while statement, product <=
100, becomes false. This terminates the iteration, and the final value of product is
243. Program executeion continues with the next statement after the while.

The following Python source code as in Figure 3.2 demonsotrate a class-


averaging program with sentinel-controlled iteration aproach, using while
iteration statement. In the program, special value called a sentinel value (also called a
signal value, a dummy value, or a flag value) to indcate the end of data entry. The user
types grades untill all legitimate grades have been entered. Clearly, the sentinel value

2
must be chosen so that it cannot be confused with an acceptable input value. Because
marks on a quiz are normally nonnegative integers, -1 is an acceptable sentinel value
for this problem.

# initialization phase
total = 0
counter =0

# get first grade from user


print("Enter grade, -1 to end: ")
grade = int(input())

# loop while sentinel value not yet read from user


while grade != -1:
total = total + grade # add grade to total
counter = counter + 1 # increment counter

# get next grade from user


print("Enter grade, -1 to end: ")
grade = int(input())
total = total + grade # add grade to total
counter = counter + 1 # increment counter

# if user entered at least one grade


if counter != 0:
# calculate average of all grades entered
average = float(total /counter)

#display average with two digits of precision


print("Class average is", average)
else:
# if no grades were entered, output message
print( "No grades were entered" )

Figure 3.2: Class average program with sentinel-controlled iteration.

3.1.2 The for iteration statement


The iteration for iteration statment handles all the details of the counter-controlled
loop iteration. Figure 3.5 illustrates the for iteration statement using range. The range
function takes three values, the first one is the starting value for the counter variable (in this
case is 1), the secound value is the stopping value (in this case is 11), and the last value is
the increment value (in this case is 1). Thus, the program operates as follows. When the for
statement begis executing, the control control variable counter is defined and initialized to

3
1. Then, the loop-continuation condition counter < 11 is checked. Because the initial value
of counter is 1, the condition is satisfied, so the print statement prints the value of
counter, namely 1. The control variable counter is then incremented by 1, and the loop
begin again with the loop continuation test. Because the control variable is now equal to 2,
the final value is not exceeded, so the program performs the print statement again.

This process continues until the control variable counter is incremented to its final
value of 11, which then cause the loop-continuation test to fail, and iteration terinates. The
program continues by performing the firdt statement after the for statement (in this case,
the program simply ends).

for counter in range(1, 11, 1):


print(counter)

Figure 3.3 Counter-controlled iteration with the for statement.

The for statement is flowcharted much like the while statement. Figure 3.6 depicts the
for iteration statement flowchart and its clearly shows that initialization occurs only once
and that incrementing occurs after the body statement each it’s performed.

counter = 1

true ++counter
counter < 11 print(counter) ;

false

Figure 3.4: Flowcharting a typical for iteration statement.

4
4.0 LABORATORY ACTIVITIES

Section A:

Create a new empty file in the Visual Studio and rewrite the following C source code to its
Python equivalent:

5
Build and run the Python source code and verify your program with the following test and
validation input/output data. Let test with 4 number of employees.

Input data Expected output


Employee # Hours Rate (RM) Pay is (RM) Total payment made (RM)
Employee 1 8 4.50 36.00
Employee 2 9 4.20 37.80
140.55
Employee 3 6 5.70 34.20
Employee 4 7 4.65 32.55

Observe the result and understand the source code functionality.

while iteration statements


a. Executable Python source code

6
b. Output windows

7
With output interface remain exactly the same, modify the above source code and replacing
while iteration statement with the for iteration statement. Validate for iteration statements
by using the same test and validation input/output data. The expected output should be the
same as of while iteration statement.

for iteration statements


c. Executable Python source code

8
d. Output windows

9
Section B:

By using nested for iteration statement, construct a complete executable Python program to
create a 2-dimensional lists with the size of 3 x 3 (3 row and 3 columns). All elements in the
lists should initialized to zero at the beginning of the program executions. By using the input
function, user will enter the value for very single element in that 2-D lists. Your program also
need to display the final 3x3 lists as input by the user.

Test and validate your program with following 3x3 lists elements.

3 2 5
4 7 11
6 9 15

a. Executable Python source code

10
b. Output window

11
5.0 Laboratory Assessment Rubrics

Course BEEL1234 TECHNOLOGY SYSTEM PROGRAMMING 2


Programme BEEL
Semester/ Session SEM 2, 2020/2021
Lab 2 Matrix ID MARK

Students' Names 2

Very Weak Weak Fair Good Excellent


Assessment Level Assessment Criteria STUDENT 0 1 2 3 4 Weightage Score Marks
1
P1 Ability to relate between
2 0.13
(Awareness) theory and practical
L E 3
ax
1
bp Ability to shows proper
P2
o e standard 2 0.25
(Readiness)
r r in C program. 3
a i
tm Construct program 1
o e P3 codes that free from
2 0.25
r n (Attempt) syntax/logic or runtime
y t errors. 3
1
P4 Display the output/result of
2 0.63
(Basic Proficiency) experiment.
3

ACTAUL MARK (5%)

1
6.0 VIDEO AND REPORT PREPARATION AND SUBMISSION

1. Laboratory activities should be done in group of two.


2. You are required to prepare a video presentation of not more than five minutes that demonstrate and explain your answers concisely
for all the tasks in the laboratory activities.
3. Both of the members must present and must show their face during the video presentation. Example of the video presentation:
https://www.youtube.com/watch?v=RA53rou1NuM
4. Please submit the video in .mp4 file via the Google Drive URL (will be given later to the class representative).
5. Fill up the laboratory sheet (must be handwritten), scan and merged the laboratory sheets into a single PDF file.
6. Please submit the PDF file via the Google Drive URL (will be given later to the class representative.

2
1
1

You might also like