Programming Logic: Loops and Algorithms
Programming Logic: Loops and Algorithms
cont = 5
soma = 0
whilecont <= 25:
soma = soma + count
cont = cont + 5
print(sum)
Regarding the algorithm presented and its functioning, select the alternative
CORRECT:
Note: 10.0
A In the code, line 5 could be replaced by the instruction cont += 1, without harming its function.
The correct would be cont += 5
C The algorithm presented aims to calculate and display on the screen the sum of all
You selected this alternative (C)
You got it right!
D The presented algorithm aims to calculate and display on the screen the sum of all
The step is 5.
E The variable contains an accumulator variable, while the variable sum is a counter.
Each concept is placed backward.
I - The structure works in the same way as the while, that is, while
a condition remains true, the loop continues executing.
II - The lasso is commonly used in situations where the number of iterations is
finished and well defined.
III - The loop works with an implicit loop control variable.
Regarding the statements presented, mark only the CORRECT ones:
Note: 10.0
A I, only.
B II, only.
C I and II only.
x = 10
whilex <= 100:
print(x)
However, the presented code contains a problem. Mark the alternative that
Correctly explain what the problem is and the solution to it.
Note: 10.0
A The problem in the code is that the control variable is not being iterated, creating an infinite loop.
close. The solution to the problem is to remove the print line from the while loop, placing it
loop to iterate the control variable.
B The problem in the code is that the logical expression placed in the loop is incorrect. The solution for
loop for x >= 100.
C The problem in the code is that the control variable is not being iterated, creating an infinite loop.
Close. The solution to the problem is to change the loop condition to x >= 100.
D The problem in the code is that the control variable is not being iterated, creating an infinite loop.
Close. The solution to the problem is to add a line that increments the control variable.
inserted before oprint.
E The problem in the code is that the control variable is not being iterated, causing an infinite loop.
close. The solution to the problem is to add a line that increments the counter variable.
after the print.
You indicated this option (E)
A for i in range(26):
print(i)
B for i in range(7, 26):
print(i)
I - The instruction 'continue' is capable of making the loop ensure that the
the next iteration will happen, as it ignores the next logical test of the loop.
II - The break instruction is capable of making the loop return to its start, restarting it.
loop.
III - It is allowed to combine break structures within the same
loop
Regarding the statements presented, mark only the CORRECT ones:
Note: 10.0
A I, just.
B II, just.
C III, only.
You marked this alternative (C)
You got it right!
A The internal for loop will be executed 10 times during the entire execution of the program.
The inner loop runs 5 x 10 = 50 times
C If we changed the external loop to a while type, the line of code corresponds to the
it is also necessary to include two more lines for initialization and increment of the variable
You marked this alternative (C)
You got it right!
Correct. Of course, to work with the while it would be necessary to add two more lines, one
increasing. However, the requested line is correctly changed to a while
x=5
print(x)
x += 5
print(x)
x += 5
print(x)
x += 5
print(x)
x += 5
print(x)
A x=5
whilex <= 25:
print(x)
x += 5
You selected this option (A)
You got it right!
B x=5
while x < 25:
print(x)
x += 5
Note: 10.0
A i = 100
while(i <= 1000):
print(i)
i += 10
B i = 100
while(i <= 999):
print(i)
i += 10
You selected this option (B)
You got it right!
C i = 99
while(i <= 1000):
print(i)
i += 10
D i = 99
while(i <= 999):
print(i)
i += 10
E i = 99
while(i <= 999):
print(i)