English

Rewrite the following program segment using a for loop. int a = 5, b = 10; while (b>0) {b−=2; } System.out.println (a * b); - Computer Applications

Advertisements
Advertisements

Question

Rewrite the following program segment using a for loop.

     int a = 5, b = 10;

     while (b>0)

     {b−=2;

     }

     System.out.println (a * b);

Code Writing
Long Answer
Advertisements

Solution

int a = 5, b;
for (b = 10; b > 0; b-=2) 
{
    // empty body
}
System.out.println(a * b);
  • b = 10 is moved to the first part of the for loop.
  • b > 0 remains the same in the middle.
  • The expression b-=2 is moved to the last part of the for header. Since the only action in the while loop was this update, the body of the for loop remains empty.
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×