How can I convert this for loop into a while loop?

for(int j=0;j<numberOfLevels;j++)
{
if(levelsArray[i]<levelsArray[j])
{
int temp1=levelsArray[i];
int temp2=scoresArray[i];
int temp3=starsArray[i];
levelsArray[i]=levelsArray[j];
scoresArray[i]=scoresArray[j];
starsArray[i]=starsArray[j];

levelsArray[j]=temp1;
scoresArray[j]=temp2;
starsArray[j]=temp3;

Is it possible to convert this for loop into a while loop? I have a hard time understanding how to convert one loop into another.
You just need to think about what conditions cause the loop to keep looping, and what conditions cause it to exit.
for(control variable; Boolean expression; counter)


All you need to do to convert 'for' to 'while' is take the control variable and declare/initialize it before the loop. Take the Boolean expression and use it for the 'while' statement. All you have left to do is put the counter in the body of the loop.
Topic archived. No new replies allowed.