How many times will this statment be run?

  • MHB
  • Thread starter find_the_fun
  • Start date
In summary, the program segment given has three nested for loops, with i ranging from 1 to 12, j ranging from 5 to 10, and k ranging from 15 to 8. The print statement is executed 576 times due to an off-by-one error, as the outer loop runs 12 times, the middle loop runs 6 times, and the inner loop runs 8 times. However, there is ambiguity in the question as to whether the last iteration of the loop is included, and this can vary depending on the programming language used.
  • #1
find_the_fun
148
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
  • #2
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.
 
  • #3
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
 
  • #4
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".
 
  • #5
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: 55

FAQ: How many times will this statment be run?

How do you determine the number of times a statement will be run?

The number of times a statement will be run depends on the code structure and logic. It can be calculated by analyzing the control flow of the code and identifying any loops, conditional statements, or recursive functions.

Can you provide an example of a statement being run multiple times?

One example of a statement being run multiple times is a for loop, where a specific code block is executed a set number of times. For example, a for loop with a counter starting at 1 and incrementing by 1 until it reaches 10 will run the code block 10 times.

What is the significance of knowing how many times a statement will be run?

Knowing the number of times a statement will be run is important for optimizing code and understanding the performance of a program. It can also help identify potential errors and improve the overall efficiency of the code.

Are there any tools or techniques to help determine the number of times a statement will be run?

Yes, there are several tools and techniques such as debugging, code review, and static code analysis, that can help determine the number of times a statement will be run. These can be used to identify potential bugs or inefficiencies in the code.

Can the number of times a statement will be run change during the execution of a program?

Yes, the number of times a statement will be run can change during the execution of a program. This can happen if the code includes user input or if the control flow is affected by other variables or conditions. It is important to consider all possible scenarios when determining the number of times a statement will be run.

Similar threads

Replies
4
Views
866
Replies
4
Views
4K
Replies
5
Views
929
Replies
18
Views
1K
Replies
4
Views
2K
Replies
1
Views
886
Replies
1
Views
2K
Replies
10
Views
2K
Back
Top