I created a couple for loops that print lines as such (each new number starts a new line, it doesn't show here):
1
22
333
4444
etc. until it reaches 9 then goes back down to 1.
I am supposed to translate it into both a while and do while loop and have been trying for the past hour and can't seem to do it.
public static void main(String[] args) {
for (int x=1; x <= 9; x++) {
for (int y = 1; y <=x ; y++){
System.out.print( x + "");
}
System.out.println();
}
// TODO code application logic here
for (int x=9; x >=1; x--) {
for (int y = 1; y <=x ; y++){
System.out.print( x + "");
}
System.out.println();
} int y =1;
int x = 1;
while (x <9){
while (y <=x){
y++;
System.out.print(x +"");{
}
System.out.println();
}
x++;
}
A for
loop statement has three parts in the for (init; condition; post)
. These parts are separated by semicolons. The init part specifies an initial statement, the condition is what determines if the loop body is executed or not and the post specifies a post loop statement.
You can do the same thing with a while
loop except that instead of a single statement, it is actually several statements. However a while
loop is not exactly like a for
loop since the continue
statement and how it behaves is a concern. More about that later.
A hint is that the various parts of the for
statement are separated by semicolons which are also used to separate statements in Java.
Consider the following for
loop source example.
int i;
for (i = 0; i < 5; i++) {
// for loop body
}
So you would have an init statement before the loop statement, i = 0
then the loop statement itself containing the condition, i < 5
, and as the last line in the loop before the closing curly brace, you would put the post loop condition, i++
.
The do while
is a bit more complicated because of when the while
condition is evaluated. In both the for
loop and the while
loop, the condition is evaluated and if the expression is not true then the loop body is not executed at all, it is skipped. In the case of the for
loop, the init statement is executed and then the condition is evaluated to determine if the for
loop body should be executed. Since a while
loop does not have an init statement as part of the while
statement, the condition is evaluated and if not true, the while
loop body is not executed.
A do while
loop has a condition that is not evaluated until after the first time through the do while
loop body. So the statements within the do while
loop are always executed at least once. Then the do while
condition is evaluated and if true, execution returns to the top where the do
is and the do while
loop body is executed again.
Some code of several variations of loops where the init is i = 0
and where the condition is i < 5
and the post is i++
. In all cases I have the variable i
defined out side of the loop body. In the case of a for
loop, defining the variable i
within the for
statement causes the scope of the variable i
, its visibility, to be restricted to the for
loop body which would not be the case for the other types of loops.
int i;
for (i = 0; i < 5; i++) {
// for loop body
}
int i = 0;
while (i < 5) {
// while loop body
i++;
}
int i = 0;
do {
// do while loop body
i++;
} while (i < 5);
I mentioned that what happens when the continue
statement is executed can make a difference when comparing these forms of loops. The way to think of it is that when a continue
statement is executed then there is a jump to the closing brace of the loop statement enclosing the continue
statement. So this introduces something to consider.
Look at the above examples but with a continue
statement. In all of the examples below there is a continue
statement which causes execution to skip to the end of the loop body when the variable i
has a value of 3.
With this change the for
loop will continue incrementing the variable i
because it's post, the third part of the for
statement, is executed at the end of the loop. However with the while
loop and the do while
loop, the incrementing of the variable i
is part of the loop body so when the continue
is executed skipping to the end of the loop, the increment of the variable i
is also skipped.
int i;
// first time init variable i to zero then evaluate condition
for (i = 0; i < 5; i++) { // evaluate condition and execute loop body if true
// for loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
} // at point of braces, post executed, condition evaluated
int i = 0;
while (i < 5) { // evaluate condition and execute loop body if true
// while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
i++; // variable i only incremented when this statement is executed
} // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
// do while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
// more statements which may be skipped by the continue
i++; // variable i only incremented when this statement is executed
} while (i < 5); // evaluate condition and jump to top of loop if true
You could make a change to the do while
loop while
condition to move the incrementing of the variable i
into the while
condition evaluation by using a pre-increment operator, the ++
operator, on the variable as in the following. We use the pre-increment operator because we want to increment the variable i
before we check its value.
int i = -1; // need to start at -1 since the while will increment at beginning of the loop
while (++i < 5) { // increment variable i, evaluate condition and body of loop if true
// while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
} // braces indicate end of loop so jump back to top of loop
int i = 0;
do {
// do while loop body
if (i == 3)
continue; // when i == 3 continue is executed to skip to end of loop
// more statements which may be skipped by the continue
} while (++i < 5); // increment variable i, evaluate condition and jump to top of loop if true
A for
loop is a construct that is extremely similar to a while
loop , except that it provides some extra niceties that reduce boilerplate code .
The beginning of a for
loop sets your initial variable (in your case, x), your condition clause ( x <= 9
) and your incrementor ( x++
). A while
loop does not do these things for, it simply runs a block of code while the condition clause in the ()
is met.
Converting a for
loop to a while
is simple-
for(int x = 0; x < 10; x++) {
int x = 0;
while(x < 10) {
x++;
}
The while
loop has all the same features as the for
loop, but without the syntactic sugar. This should help you convert the loops in your question, and in general.
Try this as your while loop:
int y = 1;
int x = 1;
while (x < 9) {
y = 1;
while (y <= x) {
y++;
System.out.print(x + "");
}
System.out.println();
x++;
}
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.