Find the errors in each of the following code segments and explain how to correct them.
a) ```
x = 1;
while ( x <= 10 );
x++;
}
```
b) ```
for ( y = .1; y != 1.0; y += .1 )
cout << y << endl;
```
c) ```
switch ( n )
{
case 1:
cout << "The number is 1" << endl;
case 2:
cout << "The number is 2" << endl;
break;
default:
cout << "The number is not 1 or 2" << endl;
break;
}
```
d) The following code should print the values 1 to 10.
```
n = 1;
while ( n < 10 )
cout << n++ << endl;
```
a) Error: The semicolon after the while header causes an infinite loop.
Correction: Replace the semicolon by a {, or remove both the ; and the }.
b) Error: Using a floating-point number to control a for repetition statement.
Correction: Use an int and perform the proper calculation to get the values you desire.
c) Error: Missing break statement in the first case.
Correction: Add a break statement at the end of the first case. This is not an error if you want the statement of case 2: to execute every time the case 1: statement executes.
d) Error: Improper relational operator used in the loop-continuation condition.
Correction: Use <= rather than <, or change 10 to 11.
You might also like to view...
You can use data from either an Access table or a query for a Word mail merge
Indicate whether the statement is true or false
In PowerPoint, the code for a macro is written in the Visual Basic Editor
Indicate whether the statement is true or false
Which of the following biometric authentication systems is the most accepted by users?
A. keystroke pattern recognition B. fingerprint recognition C. signature recognition D. retina pattern recognition
The goals of confidentiality and integrity are basically the same.
a. true b. false