-2
$\begingroup$

How many times is the print statement executed, wehre i,j,k ∈ Z? Explain

For i := 1 to 12 do

For j :=5 to 10 do

For k:=15 downto 8 do

Print (i-j)*k

  • 1
    OMG! When you don't know what to do, try multiplying...2012-10-10

1 Answers 1

3

Start inside and work out. The innermost loop executes once for each value of $k$ from $15$ down to $8$; that’s $15-7=8$ values, so the print statement is executed $8$ times every time the innermost loop is entered.

Now how often is the innermost loop entered? Once for each value of $j$ from $5$ to $10$, i.e., $10-4=6$ times. That is, every time the middle loop is entered, the innermost loop runs $6$ times, so the print statement is executed $6\cdot8=48$ times.

Can you finish it from there? How often is the middle loop entered?

  • 1
    @Sigur: I’m assuming that `downto` works like `to`, which in every language that I’ve used includes the limit.2012-10-10