I also remember this in form.
while(conditions){
//Processing content
}
・ Repeat processing is executed while the conditions are met. ・ The "==" and ">" that can be used under the conditions are the same as for if.
When repeating the process 5 times
int i;
i = 1;
while (i <= 5) {
System.out.println(i);
i++; //← Loop without this
}
・ Set the initial value of "i" with "i = 1" before while ・ When "i" is "5" or less, execute in ** while ** ・ Add "1" to "i" each time it is processed as "i ++" in while ・ When "i" becomes "6", it is out of the condition, so it ends.
Is it such a place? Add the ↑ one to the usual format and execute.
Run
It worked like that.
In short, the processing in while is never executed. Before while, the conditions should not be matched.
int i;
i = 6;
while (i <= 5) {
System.out.println(i);
i++;
}
Run
Since "i = 6" is set before while, the while condition is not satisfied. while is passed through.
Why did you bother to repeat 0 times? It is connected to the next "do-while". .. ..
It's similar to while, but for the time being I also remember this in the form.
do {
//Processing content
} while (conditions); // ← ;Be careful because you tend to forget
・ Repeat processing is executed while the conditions are met. ・ The "==" and ">" that can be used under the conditions are the same as for if.
Up to this point, it is the same as while, but different from while.
・ ** The processing contents in the do-while are always executed once **
Even if the conditions are not met, the process in do-while is executed once. Do you imagine that it will happen because the conditions are written at the back?
■ When the condition is met before do-while
int i;
i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Run
Well, the result is the same as while.
■ When the condition is not met before do-while
int i;
i = 6;
do {
System.out.println(i);
i++;
} while (i <= 5);
Run
Indeed, the processing in the do-while is certainly executed once.
When you want to execute the process once regardless of the conditions Use "** do-while **" instead of "while".
Perhaps the day will come when I want to do that.
Recommended Posts