How many times will this statment be run?

  • Context: MHB 
  • Thread starter Thread starter find_the_fun
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the execution count of a print statement within a nested loop structure in a programming context. Participants analyze the loop boundaries and the implications of different programming languages on the iteration counts.

Discussion Character

  • Debate/contested

Main Points Raised

  • One participant calculates the print statement execution as 12x5x7 but questions the answer key's claim of 576.
  • Another participant points out a potential off-by-one error in the interpretation of loop boundaries, questioning the execution count of the outer loop.
  • A later reply suggests that the question may be ambiguous regarding whether the loop includes the upper limit (i.e., whether it is i < 12 or i ≤ 12).
  • One participant argues that in programming languages like Fortran and Pascal, the loop structure "for i := m to n" includes the upper limit, thus running the last iteration when i equals n.
  • Another participant shares an experiment in Matlab, indicating that both the lower and upper bounds are included in the loop execution.

Areas of Agreement / Disagreement

Participants express differing views on the interpretation of loop boundaries and whether the upper limit is included, leading to multiple competing perspectives on the execution count.

Contextual Notes

The discussion highlights potential ambiguities in loop definitions across different programming languages and the implications for execution counts, but does not resolve these ambiguities.

find_the_fun
Messages
147
Reaction score
0
Consider the following program segment where i,j and k are integer variables
for i := 1 to 12 do
for j := 5 to 10 do
for k := 15 downto 8 do
printf (i-j)*k

How many time is the print statement executed?

I thought it would be 12x5x7 but the answer key has 576?
 
Physics news on Phys.org
If "for j := 5 to 10" runs 10 - 5 = 5 times, then why are you saying that "for i := 1 to 12" runs 12 times?

See off-by-one error in Wikipedia.
 
Evgeny.Makarov said:
If "for j := 5 to 10" runs 10 - 5 = 5 times, then why are you saying that "for i := 1 to 12" runs 12 times?

See off-by-one error in Wikipedia.

Isn't the question ambiguous? For example "for i := 1 to 12" you don't know if another iteration will occur at 12 i.e. you don't know if it's i < 12 or i <= 12
 
Well, I have not seen a programming language where a construction similar to "for i := m to n", without explicitly using < or ≤, runs the last iteration for i = n - 1. For example, in Fortran, the code

Code:
do i=1,10
  print*,i**2
end do

runs 10 times. The same thing happens in Pascal with "for i := 1 to 10 do".
 
Interesting question.

I did a test in Matlab and it seems to run both the lower bound and the upper bound (obviously everything in between as well).

The very inefficient code:
Code:
testmatrix = [];
for i=5:10;
    testmatrix = [testmatrix;i];
end;

Outputs:
View attachment 1619
 

Attachments

  • Screen Shot 2013-11-03 at 9.24.15 PM.png
    Screen Shot 2013-11-03 at 9.24.15 PM.png
    731 bytes · Views: 79

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K