(iv) Convert the given loop into exit controlled loop.
int a, b;
for ( a=10 , b=1; a>=1 ; a-=2)
{
b+=a;
b++;
}
System.out.print(b);
Answer:
To convert the given for loop into an exit-controlled loop (i.e., a do-while loop), we need to ensure that the loop body executes at least once before checking the condition.
Converted Code Using do-while Loop:
int a = 10, b = 1; do { b += a; b++; a -= 2; } while (a >= 1); System.out.print(b);
Explanation:
-
Initialization:
a = 10, b = 1 -
Loop Execution: The loop executes at least once before checking
a >= 1. -
Update:
ais decremented by 2 in each iteration. -
Condition Check: The loop continues as long as
a >= 1.
This ensures the loop behaves the same way as the original for loop but follows an exit-controlled structure.
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of ICSE Computer Applications Class 10 – Previous Year Question Papers & Solutions, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.