Studying Java -10

Repeat processing using while

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 pic001.JPG

It worked like that.

Repeat 0 times

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 pic004.JPG

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". .. ..

Repeating processing using 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 pic002.JPG

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 pic003.JPG

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

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying java9 (jShell)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Java
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java string
Studying Java # 6 (How to write blocks)
java (array)
Java serialization
java beginner 4
JAVA paid
Java (set)
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
[Java] Polymorphism
Java review
Java features
[Java] Inheritance
FastScanner Java
Java features
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics