1
COMP1117B Tutorial 5
LOOPS
2
About this tutorial
Break and Continue statements in loops
MC questions to check your understanding.
Hints to tutorial exercises.
3
Break and continue
Break statement:
The break statement terminates the
loop containing it.
Output
4
Break and continue
Continue statement:
The continue statement is used to skip
the rest of the code inside a loop for
the current iteration only.
Loop does not terminate but continues
on with the next iteration.
Output
5
Q1
Which of the following options is correct?
A. The loop will never end
B. The value of number will be printed exactly 1 time
C. The value of number does not change
D. The value of number will be printed exactly 5 times
6
Q1 Answer
Which of the following options is correct?
A. The loop will never end
B. The value of number will be printed exactly 1 time
C. The value of number does not change
D. The value of number will be printed exactly 5 times
7
Q2
What will the following code print?
A. 12
B. 9
C. 7
D. 8
8
Q2 Answer
What will the following code print?
A. 12
B. 9
C. 7
D. 8
9
Q3
What will the following code print?
A. 112233
B. 123123123
C. 111213212223313233
D. 112131212223313233
10
Q3 Answer
What will the following code print?
i remains constant in the
A. 112233
inner loop and j changes
B. 123123123
i = 1, j = 1, 2, 3
C. 111213212223313233 i = 2, j = 1, 2, 3
D. 112131212223313233 i = 3, j = 1, 2, 3
11
Q4
What will the following code print?
A. 2, 3, 5, 7
B. 1, 2, 3, 5, 7
C. 0, 1, 3, 5, 7, 9
D. 0, 1, 2, 3, 5, 7
12
Q4 Answer
What will the following code print?
A. 2, 3, 5, 7
B. 1, 2, 3, 5, 7
C. 0, 1, 3, 5, 7, 9
D. 0, 1, 2, 3, 5, 7
13
Q5
What will the following code print?
A. “Hi” is printed 3 times
B. “Hi” is printed 5 times
C. “Hi” is printed 6 times
D. “Hi” is printed 15 times
14
Q5 Answer
What will the following code print?
i = 0, print “Hi” 3 times
A. “Hi” is printed 3 times i = 1, print “Hi” 3 times
B. “Hi” is printed 5 times
i > 1, the j-loop is stopped before
C. “Hi” is printed 6 times
D. “Hi” is printed 15 times printing
15
Q6
What will the following code print?
A. “Hi” is printed 15 times
B. “Hi” is printed 10 times
C. “Hi” is printed 6 times
D. “Hi” is printed 5 times
16
Q6 Answer
What will the following code print?
j can be 0, 1
A. “Hi” is printed 15 times
The outer loop (i-loop) was performed 5 times
B. “Hi” is printed 10 times In each i-loop, “Hi” is printed 2 times
C. “Hi” is printed 6 times
D. “Hi” is printed 5 times
17
Q7
What will the following code print?
A. abcd
B. 012
C. 0
D. ab
18
Q7 Answer
What will the following code print?
It is comparing the
ASCII code of the
characters
A. abcd
B. 012
C. 0
D. ab
19
Q8
What will the following code print?
A. abcd
B. 012
C. 0
D. ab
20
Q8 Answer
What will the following code print?
A. abcd
B. 012
C. 0
D. ab
21
Tutorial Exercise 5.1
22
Tutorial Exercise 5.1
Notice that the input number is stored as a string. We can use
for loop to get each digit of this number.
Iterate over characters of a string
23
Tutorial Exercise 5.2
24
Tutorial Exercise 5.2
Hints
There are two ways to handle the situation when there is no solution.
• Using a flag (Boolean) to record whether the solution has been found. flag is
set to False before the iteration and will be changed to True when the solution
is found.
• The loop needs to be stopped when the solution is found.
If the number of rabbits is greater than the number of heads at the end of the
loop, then there is no solution.
25
Tutorial Exercise 5.3
26
Tutorial Exercise 5.3
The next number in the sequence is defined as:
Hints
27
Tutorial Exercise 5.4
28
Tutorial Exercise 5.4
A palindrome number reads the same backwards and forward.
Assuming the input is 2024
To get the rightmost digit, use % operator
2024 % 10 = 4
To remove the rightmost digit, divide it by 10
2024 / 10 = 202
By using a loop, you can turn the number backwards.
29
Tutorial Exercise 5.4
Inputs Expected output
1 4
1
2
2
2
2
3
3
3
0
30
Tutorial Exercise 5.4
Iterate over the numbers
Record the last number entered so that we can
know if a subsequence continued.
Use a counter to record the length of the
subsequence that all the numbers are equal.
Reset the counter when a new subsequence
begins.
Record the max_length when this subsequence is
ended.
31
Q&A
Please feel free to post on Moodle Forum if you have questions.
But don’t copy and paste your own code to the Moodle forum.
You may also contact your Student TA for help.