Do while to for conversion – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2017-03D #JAVA#7237    siteicon   siteicon  

Problem Statement - Do while to for conversion

Convert following do-while loop into for loop.

int i=1;
int d=5;
do{
  d=d*2;
  System.out.println(d);
  i++;
}while(i<=5);

Solution

TC++ #7237

for(int i=1, d=5; i<=5; i++)
{
d = d * 2;
System.out.println(d);
}

Notes

  • do-while to for conversion may not always be perfect as do while loop does run at least once while the for loop may not run even once based on the initial values and test conditions.


Share

CSKC| Created: 15-Apr-2019 | Updated: 27-Nov-2019|ICSE2017









Back