recategorized by
2,242 views
4 votes
4 votes

Consider the two program segments below:

  1. for
        i:=1 to f(x) by 1 do
        S
    end
  2. i:=1;
    While   i<=f(x) do
            S
            i:=i+1
    end
    

Under what conditions are these two programs equivalent? Treat $S$ as any sequence of statements and $f$ as a function.

recategorized by

2 Answers

3 votes
3 votes
In both program segment if f(x) returns same value
1 votes
1 votes
The solution is bit dependent on what the for loop means in this context . Whether the for loop runs for N times or N-1 times defines the solution.

If for loop runs for N-1 times :

For loop runs for f(x) - 1 times and while loop runs for f(x) times . Hence the only way these programs can be equivalent if none of the programs run i.e if f(x) < 1

If for loop runs for N times :

it's always identical

Related questions

8 votes
8 votes
1 answer
1
go_editor asked Dec 19, 2016
1,484 views
Below figure is the flow-chart corresponding to a program to calculate the $\gcd$ of two integers, $M$ and $N$ respectively, $(M, N >0).$ Use assertions at the cut point ...
0 votes
0 votes
1 answer
3
go_editor asked Dec 19, 2016
463 views
Write a LISP function to compute the product of all the numbers in a list. Assume that the list contains only number.
0 votes
0 votes
0 answers
4
go_editor asked Dec 19, 2016
465 views
Which of the following features are available in Ada?procedures, monitors, packages, common statement, goto statement, generic unit tasks, backtracking, recursion, except...