简体   繁体   中英

Change for-loop to a while

How can I change the following code to while-loop?

for (int i = 12; i < 40; i += 14) {
    for (int j = 30; j > 10; j -= 9) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
    }
}

I tried this but it's not working:

public class WhileExample {
    public static void main(String[] args) {
        int i = 12;
        int j = 10;
        while (i <= 40) {
            while (j <= 30) {
                System.out.println(i + "+" + j + "=" + (i + j) + "");
                j++;
            }
            i++;
        }
    }
}

The answer you looking for:

int i = 12;
while (i < 40) {
    int j = 30;
    while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
    }
    i += 14;
}

You have a few issues here. First, you need to retain the same condition in the for and while loops. Second, you need to use the same increment the for loop uses, not just a ++ call. Third, you aren't initializing j like it is in the loop, and the initialization should be done inside the outer loop:

public class WhileExample {
    public static void main(String[] args) {
        int i = 12;
        while (i < 40) {
            int j = 30;
            while (j > 10) {
                System.out.println(i + "+" + j + "=" + (i + j) + "");
                j -= 9;
            }
            i += 14;
        }
    }
}

Following is the working code to your problem where you aren't seeting up again the value of j to it's default value,

public class WhileExample {

  public static void main(String[] args) {
    int i = 12;
    int j = 30;
    while (i < 40) {
      while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
      }
      i += 14;
      j = 30;
    }
  }
}

To convert ANY for loop into a while loop, you have to understand the anatomy of both loops. In general, both loops are constructed as follows:

for-loop

for (loop counter initialization; break condition; counter increment/decrement) {
    body of loop
}

while-loop

while (break condition) {
    body of loop
}

The only thing missing in the while loop is the counter initialization and the counter decrement/increment. Typically, when you require loop counters, you will use a for loop. When you don't require a loop counter, you will use a while loop. BUT, that is not a rule enforced by the language. You can do something like this to use a while loop with a counter variable:

int counter = 0;
while (break condition) {
    body of loop
    counter++;
}

Or, if you want to use a for loop without a counter:

for (;break condition;) {
    body of loop
}

Or even something crazier like this:

for (;;) {
    body of loop
    if (break condition) {
        break;
    }
}

OBVIOUSLY, the best thing is to use the loops for their intended purpose: for-loops when loop counters are required and while-loops when they are not.

Based on the information here, you should be able to see that:

for (int i = 12; i < 40; i += 14) {
    for (int j = 30; j > 10; j -= 9) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
    }
}

Should translate to:

int i = 12;

while (i < 40) {
    int j = 30; // initializing this outside the outer `while` is a logic error
    while (j > 10) {
        System.out.println(i + "+" + j + "=" + (i + j) + "");
        j -= 9;
    }
    i += 14;
}

The easiest way to achieve this conversion is to start from the inside out. Meaning, go to the innermost loop, convert it following my suggestion, and work your way out. No matter how nested your loop is, you should be able to convert any for into a while loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM