J. Nav J. Nav - 1 month ago 5
Java Question

JAVA: For Loop to Do-While Loop

My original for loop is:

public static void main (String args[]) {
int i;
for (i=500; i >= 0; i -= 10) {
System.out.println(i);
}
}


I want to change it to a do-while loop. This is what I have so far:

public static void main(String[] args) {

int i = 500;
do {
System.out.println(i);
} while (--i >= 0; i -= 10);
}


Obviously, this is wrong, but how am I supposed to insert the decrement of 10 into the do-while loop?
Thanks for all of your help ahead of time!

Answer
public static void main(String args[]) {
    int i;
    for (i = 500; i >= 0; i -= 10) {
        System.out.println(i);
    }

    i = 500;
    do {
        System.out.println(i);
    } while ((i-=10) > 0);
}
Comments