How many times will this statment be run?

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

The program segment in question executes the print statement 576 times. This is derived from the nested loops where the outer loop runs 12 times (for i from 1 to 12), the middle loop runs 6 times (for j from 5 to 10), and the innermost loop runs 8 times (for k from 15 downto 8). The total execution count is calculated as 12 * 6 * 8 = 576. The confusion arises from the interpretation of loop boundaries, which are clarified by examining the behavior in languages like Fortran and MATLAB.

PREREQUISITES
  • Understanding of nested loops in programming
  • Familiarity with loop boundary conditions in languages like Fortran and MATLAB
  • Basic knowledge of integer variable types and operations
  • Experience with programming syntax in Pascal or similar languages
NEXT STEPS
  • Study loop constructs in Fortran and MATLAB for better understanding of iteration limits
  • Learn about off-by-one errors and how they affect loop execution
  • Explore performance implications of nested loops in programming
  • Investigate the differences in loop behavior across various programming languages
USEFUL FOR

Programmers, computer science students, and educators looking to deepen their understanding of loop constructs and execution counts in programming languages.

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: 78

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