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?
Assume all variables have been declared, but not necessarily initialized.

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;
do
x++;
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!

Computer Science & Information Technology

You might also like to view...

A(n) ____________ is a constrained version of a linked list in which nodes can be inserted only at the end of the list and deleted only from the start of the list.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

When publishing a Web site, you can choose to ____________________ the files, meaning that Expression Web will copy whichever version of the file is newer to the opposite site so that you end up with a matching set of the newest version of all files at the end.

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology

A method's identifier must be more than one word, must have embedded spaces, and can be a Java keyword.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

A virtual table in a database is known as a __________.

a. Table b. Record c. Field d. Key e. View

Computer Science & Information Technology