a> b. Then we can define compare() using a case analysis. . compare(a, b) .
case a < b . result := - . case a = b . result := .
else -- a > b . result : = . . Iterative statement An iterative process executes the same action repeatedly, subject to a condition C.
If C is a condition and S is a statement, then while C S is a statement, called an iterative statement, that describes the following action: . Test whether C is true or false. . If C is true, then do S and go back to step ; otherwise do nothing.
The iterative statement is commonly known as a loop. These two steps, testing C and executing S, are repeated until C becomes false. When C becomes false, the loop ends, and the control flows to the statement next to the iterative statement. The condition C and the statement S are called the loop condition and the loop body, respectively.
Testing the loop condition and executing the loop body once is called an iteration. not C is known as the termination condition. Iterative control flow is depicted in the flowchart of Figure . .
Condition C has two outgoing arrows, true and false. The true arrow points to S box. If C is true, S box is executed and control flows back to C box. The false arrow points to the box after the iterative statement (dotted box).
If C is false, the loop ends and the control flows to the next box after the loop. C S true false Figure . : Iterative control flow Example . .
Construct an iterative algorithm to compute the quotient and remainder after dividing an integer A by another integer B. We formulated the specification of the algorithm in Example . as divide (A , B) -- inputs: A is an integer and B ≠ -- outputs : q and r such that A = q X B + r and -- ≤ r < B Now we can construct an iterative algorithm that satisfies