Here is a collection of while and do-while statements. Identify: i. those that are correct, and are likely to give the programmers intent; ii. those that are correct, but unlikely to give the programmer's intent, and iii. what compiler error will the rest generate?
```
a) cin >> n;
while (-1 != n)
{
sum = 0;
sum = sum + n;
}
b) cin >> value;
while ( value != -1 )
sum = sum + value;
cin >> value;
c) cin >> n;
int i = 1,
>>Semicolon not comma
while ( i < n );
sum = sum + i;
i++;
d) cin >> count >> limit;
do
count++
while ( count ??count > limit );
e) cin >> x;
dox++;
while( x > x );
```
a) This compiles. It is unlikely to be what the programmer intended. What the intent
might be is hard to guess, since the value of sum is reset to 0 each time the loop
executes.
b) This compiles. The indentation suggests that the programmer intended the two
lines following the to be in the body of the loop. The second of these is not
controlled by the while clause, resulting in an infinite loop .
>>Something is wrong. Maybe the indentataion??
c) This compiles. There are two intent errors evident: the semicolon on the second
line and the missing braces. If the loop is entered, this is an infinite loop, since the
statement controlled by the loop is the null statement inserted by the semicolon on
the second line changes neither i nor n. The intent evidently was to run the loop
n or n-1 times.
d) The syntax error is a semicolon missing from count++.
e) This compiles, but the loop executes its body only once. It is difficult to guess
what the programmers intent might have been, but this probably isn't it!
You might also like to view...
What is the technology that enables a user to authenticate to a company network from their assigned workstation and then be able to interact with resources throughout the private network without needing to enter additional credentials?
A. Single sign-on B. CHAP C. Multifactor authentication D. AAA services
This is a spoke-to-spoke network. In this secure VPN network, data between sites is exchanged between sites without requiring data traffic to pass through an organization's virtual private network.
What will be an ideal response?