Java while and for statements

while statement

The while statement is used when you want to automatically iterate a certain process. For example, if you want to output numbers from 1 to 100 to the console, writing System.out.println () 100 times will make the code redundant. Therefore, we use a while statement. As a flow of iterative processing ① Variable initialization ② Conditional expression ③ Repeated processing ④ Variable update From there again ② Conditional expression ③ Repeated processing ④ Variable update And repeat.

while (conditions) {
Repeated processing;

You can use the while statement above to perform iterative processing. [Example]

int x = 3;
  while (x <= 3) [
  System.out.println(x + "Rank");

The above defines the variable x with x = 3. x ++; adds 1 to x. Since 1 is added to the variable, the while condition becomes false at the 4th iteration, and the iteration process ends.


int x =5;
  while (x > 0) [

The above iterates when is greater than 0.

for statement

Along with the while statement, the for statement is one of the iterative processes. The flow of iterative processing is the same as the while statement ① Variable initialization ② Conditional expression ③ Repeated processing ④ Variable update From there again ② Conditional expression ③ Repeated processing ④ Variable update And repeat. for statement

for (Variable initialization;Conditional expression;Variable update) {
  System.out.println(Repeated processing); 


for (int x = 1;x <= 10;x++) {

If you write as above, the process will be repeated 10 times. If you use it, the for statement is easier to use. continue Break is used to end the iterative process, but continue skips only that lap and executes the next lap. [Example]

for (int x = 1;x <= 10;x++) {
  if(x%5==0) {

Written as above, it ends the loop of multiples of 5 and executes the next loop.

Recommended Posts

Java while and for statements
[Java] for statement, while statement
Java if and switch statements
[Java] Summary of for statements
New grammar for Java 12 Switch statements
AWS SDK for Java 1.11.x and 2.x
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
Classes and instances Java for beginners
Utilization of Java array elements, for, length, value, and extended for statements
For JAVA learning (2018-03-16-01)
[Java] for Each and sorted in Lambda
[For beginners] Difference between Java and Kotlin
XXE and Java
[Java] Proxy for logging SQL and SQL results
Java, for statement / while statement starting from beginner
Java for statement
Review notes for Java 1.7 and later file copies
I studied for 3 weeks and passed Java Bronze
Kantai Collection Java # 1 Classes and Objects [For Beginners]
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] for statement / extended for statement
[Java] String comparison and && and ||
Countermeasures for Java OutOfMemoryError
Java --Serialization and Deserialization
[Java] Arguments and parameters
(Memo) Java for statement
NLP for Java (NLP4J) (1)
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Sample (Java) for OAuth 2.0 authentication and access token acquisition
Compile with Java 6 and test with Java 11 while running Maven on Java 8
VSCode Java Debugger for Java Build failed Causes and countermeasures
Learn for the first time java # 3 expressions and operators
This and that for editing ini in Java. : inieditor-java
List of frequently used Java instructions (for beginners and beginners)
Prepare the environment for java11 and javaFx with Ubuntu 18.4
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
Study Java # 2 (\ mark and operator)
Store in Java 2D map and turn with for statement
[For beginners] Explanation of classes, instances, and statics in Java
[Java] Make variables in extended for statement and for Each statement immutable
About for statement and if statement
Java update for Scala users
Java conditional branching: How to create and study switch statements
[Java] Difference between == and equals
[Java] Stack area and static area
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
[Java] Generics classes and generics methods
Java encryption and decryption PDF
[Java] Precautions for type conversion
Java and Iterator Part 1 External Iterator
About Java variable declaration statements
Books used for learning Java
2018 Java Proficiency Test for Newcomers-Basics-