for i = 1 to n do
for j = 1 to i do
print “hello world”
end for
end for
I don't understand what to
and do
mean and what exactly this code does. The question was, how often is "hello world" printed out when n = 4
. Is the answer 10? How do I work this out?
Outer Loop:
for i = 1 to n do
(initially i
will be assigned to 1)
this loop is saying that something has to be done n times (meaning of to). What has to be done is written in your inner loop (meaning of do).
for j = 1 to i do
print “hello world”
For each value of i, the inner loop will be executed i
times (this is because each time when i
changes, your inner loop will be executed i
times (for j = 1 to i do)
). Here, it will print hello world
each time.
Hope it helps you