[Java] for statement, while statement

A note of what I learned about loop statements.

for statement

Use when you want to repeat the same process a specified number of times using a loop counter or the like. ** ** It is often used for commands such as ** "Output from 1 to 100!" ** Like fizz buzz. It is also compatible with the access of array elements by subscripts, and is also suitable for referencing the contents of an array one by one.

[Format]

for (Initialization formula;Conditional expression; change expression){
Statement to execute;
Statement to execute;
...
}

[Example using a for statement]

(Declaration and initialization of String array)
String[] fighter = {Zero Fighter,Spitfire,Messerschmitt,Lavo chicken};

(Show all the contents of the array arr)
for (int i = 0; i < fighter.length; i++) {                                      
    System.out.println("fighter[" + i + "] … " + arr[i]);
}

【Execution result】

fighter[0]…Zero Fighter
fighter[1]… Spitfire
fighter[2]… Messerschmitt
fighter[3]… Lavo chicken

while statement

** Iterative syntax controlled only by conditional expressions. ** ** Use it properly with the for statement when the number of repetitions such as ** "until the user inputs something" ** is not fixed. In some cases, it may end without moving to make the first judgment.

[Format]

while(Conditional expression){
Statement to execute
}

[Example using a for statement]

//Variable initialization
int i = 1;

//Output numbers from 1 to 10
while (i  <= 10){
  System.out.println( i );
  i++; //If you forget this, an infinite loop will occur, so be careful!
}

【Execution result】

1
2
3
4
5
6
7
8
9
10

About break

You can break out of the loop at that point by writing a break statement.

[Format]

In the if statement etc. ** "When the condition is met, execute the break statement to get out of the loop" **.

for (Initialization formula;Conditional expression; change expression) {
    if (Conditional expression to exit the loop) {
        break; //Exit the loop if the conditions are met
    }
}

[Example using a for statement]

Iterative statements that output numbers from 1 to 10 are stopped when break reaches 7.

    for (int i = 0; i < 10; i++) {
      System.out.println(i);
      if (i == 7) {
        break;
      }
    }

【Execution result】

1
2
3
4
5
6
7

continue statement

The continue statement skips further processing at that point.

[Format]

while (Conditional expression) {
    [Process 1]
    if (Skip condition) {
        continue;   //Process 2 is skipped if the conditions are met
    }
    [Process 2]
}

[Example using continue statement]

Outputs numbers from 1 to 10, and skips processing if it is a multiple of 4 (4 and 8).

    for (int i = 1; i <= 10; i++) {
      if (i % 4 == 0) {
        System.out.println("It is a multiple of 4.");
        continue;
      }
      System.out.println(i);
    }

【Execution result】

1
2
3
It is a multiple of 4.
5
6
7
It is a multiple of 4.
9
10

I think that if you master it, you will be able to express quite complicated things. The infinite loop will be output at a later date.

Recommended Posts

[Java] for statement, while statement
Java for statement
[Java] for statement / extended for statement
(Memo) Java for statement
Java, for statement / while statement starting from beginner
[Java] Basic statement for beginners
Java while and for statements
Java learning memo (while statement, do-while statement)
For JAVA learning (2018-03-16-01)
2017 IDE for Java
Java switch statement
Complex for statement (basic)
Java instruction execution statement
[Java] Package for management
Use Java7 try-with-resources statement for Cursor close processing
Countermeasures for Java OutOfMemoryError
NLP for Java (NLP4J) (2)
NLP for Java (NLP4J) (1)
# 3 [Note] do ~ while statement
[Java basics] Let's make a triangle with a for statement
Java (while statement) basic problem that beginners did first
I tried using an extended for statement in Java
[Beginner] Java variable / logical operator / extended for statement [Note 22]
How to loop Java Map (for Each / extended for statement)
About for statement and if statement
Isn't the While statement evil?
Java update for Scala users
12 Corresponds to the while statement
Java debug execution [for Java beginners]
[Java] Precautions for type conversion
Books used for learning Java
2018 Java Proficiency Test for Newcomers-Basics-
Java programming basics practice-for statement
Points for refactoring (if statement)
☾ Java / Iterative statement and iterative control statement
Java thread safe for you
[Java] Summary of for statements
Java programming basics practice-switch statement
Java for beginners, data hiding
[Java] Tips for writing source
Java installation location for mac
Java application for beginners: stream
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Get Map key / value pairs using extended for statement
Java review ③ (control syntax [if / switch / while / for], conditional expression)
C # cheat sheet for Java engineers
New grammar for Java 12 Switch statements
[For beginners] Summary of java constructor
AWS SDK for Java 1.11.x and 2.x
Rock-paper-scissors game for beginners in Java
[Java] Branch enum with switch statement
Java for beginners, expressions and operators 1
[Java] Memo for naming class names
[For beginners] Run Selenium in Java
Hello World for ImageJ Java Plugin
[OpenCV3.2.0] Eclipse (Java) settings (for Mac)
Java for beginners, expressions and operators 2
[Java] Really scary switch statement story
Enable OpenCV with java8. (For myself)
Spring Framework tools for Java developer