Warm tip: This article is reproduced from stackoverflow.com, please click
algorithm big-o time-complexity

can someone please explain this pseudocode for me?

发布于 2020-03-31 23:01:02
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?

Questioner
Mojibar Homan
Viewed
96
KAMLESH KUMAR 2020-01-31 20:24

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