Rewrite the following code fragment using a for loop inst in

Rewrite the following code fragment using a for loop inst int i =0; while (i

Solution

22.

for (int i=0; i<50; i+=2){

    System.out.println(i);

}

23.

This will output all even numbers from 0 to 48. Hence, the output is:

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48

24. The three ways to control a loop.

1. Set condition for termination in while loop. For example while (i<5) will break once i is equal to 5.

2. Set condition for termination in for loop. For example for(i=0;i<5;i++) will iterate from 0 to 4 and then on the sixth iteration will not satisfy the given condition (i<5) and will break.

3. Use break statement if condition is met.

For example,

while(true){

    if(i==5) break;

    i++;

}

This will break the while loop once i equals 5.

 Rewrite the following code fragment using a for loop inst int i =0; while (iSolution22. for (int i=0; i<50; i+=2){ System.out.println(i); } 23. This will ou

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site