##### Question

In: Computer Science

# Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where...

Find a loop invariant (I) for the following while loop with the post-condition

{Q: S=1}, where S is an integer and the operator / represents an integer division.

while (S > 1) do
S = S / 2;
}

## Solutions

##### Expert Solution

Loop invariant can be defined as a condition which will always be true. This property must be satisfied throughout every iteration of the loop.

We have the post-condition:

Q: S = 1

The given loop checks if the value of 'S' is greater than 1 or not. If it is true, then it divides 'S' by 2 and this process is repeated in every iteration. The while loop condition will fail when the value of 'S" is one or less. At that case, the value of 'S' will never be less than one as it is integer in nature. Loop invariant does not depend on the state of the iterative condition. It always remains true irrespective of the iterative condition which can be true or false.

The post-condition must be true after the execution of the while loop condition.

So, one loop invariant we can determine from the given relation is:

S > 1 [the while loop will be executed only when 'S' is greater than one, not equal to one]

Please comment in case of any doubt.

## Related Solutions

##### According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that...
According to the loop invariant theorem, a valid loop invariant ?(?)I(n) must have the property that Select one: a. ?(?)=0I(n)=0 after a finite number of steps. b. After the loop, ?→?(?)P→I(N) where ?P is the postcondition. c. ?(0)→?I(0)→Q, where ?Q is the precondition. d. None of the other answers are correct
##### Write the loop invariant for the following program the computes quotients (q) and remainders (r) of...
Write the loop invariant for the following program the computes quotients (q) and remainders (r) of “a” divided by ”d”. Then prove all three cases (Initialization, Maintenance, Termination). Assume “a” and “d” are positive integers. r=a q=0 while r>=d             r=r-d             q=q+1
##### How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
##### (C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
##### Why is an exit condition so important for a while loop?
Why is an exit condition so important for a while loop?a.An exit condition is not vital.  A while loop works fine without one.b.So the programmer knows when it is okay to leave the room.c.If no exit condition is provided, while loops cannot continue beyond one iterationd.It allows the loop to end instead of running indefinitely.e.While loops are unstable, and must know how to exi
##### Which of the following statements is TRUE? A for loop executes indefinitely if the loop condition...
Which of the following statements is TRUE? A for loop executes indefinitely if the loop condition is always false. If the loop condition in a for loop is initially false, the loop body does not execute. C++ does not allow you to use fractional values for loop control variables that are real numbers. In a for statement, if the loop condition is omitted, it is assumed to be false.
##### I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
##### Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
##### Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3};...
Here is a traditional loop in C:   while (A{i} == 0)                A{i} = A{5} + A{i+3}; (NOTE: please consider braces {} here as usual square brackets for array index). Assume that i is stored in register $s1, and the base address of the array A is in$s2. Fill in the multiple blank spaces in the following MIPS program that is supposed to be compiled from the above C loop: loop: sll $t0,$s1,                add $t0,$t0,                lw $t1, 20($s2)...
##### Given a set S = {1, -1, i, -i} where i2 = -1 and with multiplication...
Given a set S = {1, -1, i, -i} where i2 = -1 and with multiplication * on this set, complete the following Cayley table: * 1 i -1 -i 1 1 -1 -i i i -1 -1 -i (b) Determine whether, or not, the operation * is a binary operation on S. (c) Is * commutative on S? (d) Investigate the following properties of binary operations for this operation on S: i. Closed ii. Identity iii. Inverse iv. Associativity