Ask your own question, for FREE!
Computer Science 7 Online
xjezzer:

Write a program using a for loop, while loop, and a do while loop all within the same program. The program will output the following numbers 3 times, one for each type of loop, including the string preceding each loop as below.

xjezzer:

The “for” loop produces! 5 4 3 2 1

xjezzer:

The “while” can do the same thing! 5 4 3 2 1

xjezzer:

Don’t forget about the “do while” loop. It can do the same thing! 5 4 3 2 1

xjezzer:

programming language is java

Extrinix:

So let’s start off with what all of these loops start with. ———————— public class Main { public static void main(String[] args) { } } ————————

Extrinix:

Now let’s look at our `for` loop, `while` loop, and `do while` loops. FOR loop: ———————— public class Main { public static void main(String[] args) { for (int i = ?; i > ?; i??) { System.out.println(i); } } } ———————— Look at that closely and let’s piece this out, (int i = ?;- this describes what the value of i is, which for 54321 needs to be valued at 5 i > ?; - this shows what the loop should do, “i is greater than ?”, so this means that ? should be 0 so the loop counts down i??) - this can have 2 values, positive (++) and negative (--), and to go from 5 to 0, you subtract, so we need -- So you get this ———————— public class Main { public static void main(String[] args) { for (int i = 5; i > 0; i--) { System.out.println(i); } } } ———————— And would print out: `5` `4` `3` `2` `1`

Extrinix:

WHILE loop: ———————— public class Main { public static void main(String[] args) { int i = ?; while (i > ?) { System.out.println(i); i??; } } } ———————— This one will simply run through the printout code and then go down a number, using the same portions (below) int i = ?;- this describes what the value of i is, which for 54321 needs to be valued at 5 like FOR loop was i > ?; - this shows what the loop should do, “i is greater than ?”, so this means that ? should be 0 so the loop counts down i??) - this can have 2 values, positive (++) and negative (--), and to go from 5 to 0, you subtract, so we need -- So you get this ———————— public class Main { public static void main(String[] args) { int i = 5; while (i > 0) { System.out.println(i); i--; } } } ———————— And would print out: `5` `4` `3` `2` `1`

Extrinix:

WHILE loop: ———————— public class Main { public static void main(String[] args) { int i = ?; do { System.out.println(i); i??; } while (i > ?); } } ———————— Same as the others, except it goes, “Do [action: printout 5/subtract 1] - while [value: i is greater than 0] So you get this ———————— public class Main { public static void main(String[] args) { int i = 5; do { System.out.println(i); i--; } while (i > 0); } } ———————— And would print out: `5` `4` `3` `2` `1`

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!