5.1 While loop
g = 21
, the condition will evaluate to
False
for the first time.
f
's initial value. Then, at each iteration, g
's value is printed, and g
's value throughout the code's execution will be
1
,
2
,
3
,
5
,
8
, and
13
. Thus, all printed values are
1
,
1
,
2
,
3
,
5
,
8
, and
13
.
n = 0
; therefore, the value of n
after the loop equals
0
. The last printed line will be: value of n after the loop is 0
.
n
equals
4
and the value of n
is only increasing, the loop condition will always remain true. The loop will never terminate.
5.2 For loop
str_var
. Iterating over all characters and incrementing the value of count
variable at each iteration results in the count's
value of
8
.
count
's value is
0
, multiplying count
by any number results in the output of
0
.
c
), and prints an additional * after the character.
10
and
22
(inclusive of both):
10
,
13
,
16
,
19
, and
22
.
range()
function generates a descending sequence, beginning from -1
until -2
. Hence, the output is -1
.
start
must be larger than end
; otherwise, no sequence will be generated.
range()
function, arguments are considered as start
and end
of the sequence, and the step size will be
1
. When step size is positive, the generated sequence will be in ascending order, and hence, start
must be less than end
. Otherwise, no sequence will be generated.
5.3 Nested loops
i
and j
values, (i, j)
, that meet the inner and outer loop conditions are as follows: (1, 1), (1, 2), (1, 3), (1, 4),
(2, 1), (2, 2), (2, 3),
(3, 1), (3, 2),
(4, 1)
i
of the output (i =
1
,
2
,
3
, and
4
) contains i
times values
1
through
4
.Hence, the outer loop executes while
i <= 4
, and the inner loop iterates overj
while j
's value is less than or equal to
4
.
0
,
1
, and
2
; hence, the outer loop executes three times.
j = 0
,
1
,
2
, and
3
) for every i
's value set by the outer loop. In the given example i = 0
,
1
, and
2
; hence, the total number of times the inner loop executes is 3 * 4 = 12
.
i
displaying i
* values in the range of
0
to
3
. Hence, the code must iterate over row ID in the outer loop, and in each row, iterate over elements' ID in the range of
0
to
3
.
5.4 Break and continue
H
e
l
l
o
print
statement is executed, the
break
statement causes the loop's termination. Also, for i = 15
, the condition (i%3 == 0 and i%5 == 0)
is met, and hence the print statement will be executed exactly once.
2
or
3
. These numbers are
2
,
3
,
4
,
6
, and
8
, and thus
8
is the final i
's value.
9
6
3
0
5.5 Loop else
i
is initialized to
16
, and the
while
loop executes with i = 16
,
8
,
4
, and
2
. In each loop's iteration, the variable exp
's value is incremented by 1. Hence, at the end of the loop, exp = 4
. Since the loop terminates without interruption, the
else
statement after the loop is executed and prints 16 is 2 to the 4
.
i
's value is
7
. Hence, in the first loop's iteration, the
else
statement within the loop executes, and the
break
statement terminates the loop. Since the loop's execution is interrupted, the
else
after the loop will not execute and there will be no output.
5
, the value will be printed, and if the value is greater than
5
, the code prints "Not all numbers are less than 5.
" and the code's execution terminates.