Professional Documents
Culture Documents
1.
2.
3.
PROGRAMME 1BEEL
SECTION /
1/1 1/2
GROUP
2.0 EQUIPMENT
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
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.
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
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).
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
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.
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.
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
10
b. Output window
11
5.0 Laboratory Assessment Rubrics
Students' Names 2
1
6.0 VIDEO AND REPORT PREPARATION AND SUBMISSION
2
1
1