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-ICSE2016-03F #JAVA#4246    siteicon   siteicon  

Problem Statement - while to for conversion

Convert the following while loop to the corresponding for loop :

int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n- -;
}

Solution

TC++ #4246

The conversion to for loop would be –
for(int m=5, n=10; n>=1 ; n- -)
System.out.println(m*n);

Notes

  • Here variable m is not being changed in the loop so its initialisation can also be kept outside as
    int m=5;
    for(int n=10;n>=1;n- -)
    System.out.println(m*n);
  • The brackets of loop can be given for clarity.


Share

sunmitra| Created: 31-Mar-2018 | Updated: 16-Apr-2019|ICSE2016









Back