Java Reference
In-Depth Information
These errors often occur when you use the next-line block style. Using the end-of-line
block style can avoid errors of this type.
In the case of the do-while loop, the semicolon is needed to end the loop.
int i = 0 ;
do {
System.out.println( "i is " + i);
i++;
} while (i < 10 ) ;
Correct
4.15
Can you convert a for loop to a while loop? List the advantages of using for loops.
Check
4.16
Point
Can you always convert a while loop into a for loop? Convert the following while
loop into a for loop.
int i = 1 ;
int sum = 0 ;
while (sum < 10000 ) {
sum = sum + i;
i++;
}
4.17
Identify and fix the errors in the following code:
1 public class Test {
2 public void main(String[] args) {
3 for ( int i = 0 ; i < 10 ; i++);
4 sum += i;
5
6 if (i < j);
7 System.out.println(i)
8 else
9 System.out.println(j);
10
11 while (j < 10 );
12 {
13 j++;
14 }
15
16 do {
17 j++;
18 } while (j < 10 )
19 }
20 }
4.18
What is wrong with the following programs?
1 public class ShowErrors {
2
1 public class ShowErrors {
2 public static void main(String[] args) {
3 for ( int i = 0 ; i < 10 ; i++);
4 System.out.println(i + 4 );
5 }
6 }
public static void main(String[] args) {
3
int i;
4
int j = 5 ;
5
6 if (j > 3 )
7 System.out.println(i + 4 );
8 }
9 }
(a)
(b)
 
Search WWH ::




Custom Search