In: Computer Science
Find the error in each of the following code segments and explain how to correct it.
x = 1;
while ( x <= 10 );
x++;
}
for ( y = .1; y != 1.0; y += .1
System.out.println( y );
switch ( n ) {
case 1:
System.out.println( “The number is 1” );
case 2:
System.out.println( “The number is 2” );
break;
default:
System.out.println( “The number is not 1 or 2” );
break;
}
The following code should print the values 1 to 10.
n = 1;
while ( n < 10 )
System.out.println( n++ );
x = 1;
E1 - while ( x <= 10 ); //There shouldn't be semi
colon after while loop
Correction - while (x<=10)
E2 - //there was no opening bracket for while loop
Correction - {
x++;
}
E3 - for ( y = .1; y != 1.0; y += .1 //Here, initialize y
as float, and close the for loop bracket
Correction - for (float y=.1; y!=1.0; y+=.1)
E4 - for loop flower bracket missing
Correction - {
System.out.println( y );
switch ( n ) {
case 1:
System.out.println( “The number is 1” );
E5 - //break statement is missing
Correction - break;
case 2:
System.out.println( “The number is 2” );
break;
default:
System.out.println( “The number is not 1 or 2” );
break;
}
}
//The following code should print the values 1 to 10.
n = 1;
while ( n < 10 )
E6 - Flower bracket missing
Correction - {
System.out.println( n++ );
}
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================