Question 11 mark
What does this program print?
total = 0for i = 2 to 8 step 3total = total + inext iprint(total)
2.2.1(a), 1.2.3(c), 1.2.4(b) · Programming
What does this program print?
total = 0for i = 2 to 8 step 3total = total + inext iprint(total)
What does this program print?
n = 1count = 0while n < 100n = n * 3count = count + 1endwhileprint(count)
What does this program print?
x = 50dox = x - 20until x < 0print(x)
How many lines does this program print?
for i = 1 to 3for j = 1 to iprint(str(i) + str(j))next jnext i
When should a while loop be used rather than a for loop?
How many times does this loop print x?
for k = 5 to 1print("x")next k
What does this program print?
word = "level"rev = ""for i = word.length - 1 to 0 step -1rev = rev + word.substring(i, 1)next iif rev == word thenprint("yes")elseprint("no")endif
What is iteration?
The program below builds a sequence of numbers.
a = 1b = 1for n = 1 to 4c = a + ba = bb = cnext nprint(b)
the trace table: one row for the end of each pass, then one row for the print.
| n | a | b | c | output |
|---|---|---|---|---|
| 1 | 1 | 2 | 2 | |
the value printed if the loop runs for n = 1 to 6.
why the order of lines 5 and 6 matters.
a loop that prints every even number from 2 to 20.
What does this program print?
x = 0for i = 1 to 10if i MOD 2 == 1 thenx = x + iendifnext iprint(x)