☾ Java / Iterative statement and iterative control statement


I'm not good at it forever.

✴︎ What is a repetitive sentence?

Iterative processing is also called ** loop **, and there are roughly three types of iterative statements: ** while statement **, ** do-while statement **, and ** for statement **.

1. while statement

The ** while statement ** iterates while the specified condition is met (the result of the conditional expression is true).

Sample.java


while (Conditional expression) {
Process statement;    //The processing statement is executed when the result of the conditional expression is true
}

If there is only one processing statement, you can omit the {}. Describe the conditional expression in () after while. The conditional expression must be an expression that has a boolean value (true or false). If the result of the condition judgment is true, the processing statement enclosed in {} is executed. The processing statement can be described on multiple lines. After executing the processing statement, control is transferred to the conditional expression again.

Sample.java


public class Sample {
  public static void main(String[] args) {
    int num = 0;
    while (num < 5) {    //Repeat while num value is less than 5
      System.out.print(num + " ");
      num++;    //Add 1 to the value of num
    }
  }
}

[Execution result] 0 1 2 3 4

Iterative processing is performed while the conditional expression returns true. When the conditional expression returns false, the iteration process ends and the while statement exits.

2. do-while statement

The ** do-while statement ** repeats the process as long as the specified condition is satisfied (true), like the while statement.

Sample.java


do {
Process statement;
} while (Conditional expression);

First of all, write the processing statement by enclosing it in {} after do. After that, write the conditional expression in () after while. Like the while statement, the conditional expression must be a boolean value (true or false). While the condition judgment is true, the iterative process is executed, and when the condition judgment becomes false, the do-while statement ends. As with the while statement, {} can be omitted if there is only one processing statement. The difference from the while statement is that the while statement first performs the condition judgment and then starts the iterative process, while the do-while statement first performs the iterative process and then the condition judgment.

Sample.java


public class Sample {
  public static void main(String[] args) {
    int num = 0;
    do {    //Execution of iterative processing
      System.out.print(num + " ");
      num++;    //Add 1 to the value of num
    } while (num < 5);    //Whether the condition judgment num is less than 5
  }
}

[Execution result] 0 1 2 3 4

As mentioned above, the difference between the while statement and the do-while statement is the timing at which the condition judgment is performed. Depending on the conditions, the while statement may never be processed in the block. On the other hand, the do-while statement has a do block before the condition judgment, so the processing statement will be executed once regardless of the condition.

3. for statement

There is a ** for statement ** as the third statement to iterate. In the while statement and do-while statement, only the conditional expression was described in (), but in the for statement, the count variable indicating the number of repetitions and the update of the count variable are also described in ().

Sample.java


for (Equation 1;Equation 2;Equation 3;) {
Process statement;
}

Expression 1 declares and initializes a variable that indicates the number of iterations. This variable is sometimes called the ** counter variable **. This expression 1 is executed only the first time. The conditional expression is described in Expression 2. The conditional result, like any other iterative statement, must be an expression that has a boolean value. While the condition judgment is true, the iterative process is executed, and when the condition judgment becomes false, the for statement ends. Expression 3 describes an expression that updates the value of the counter variable. If the statement to be processed has only one minute, {} can be omitted.

Sample.java


public class Sample {
  public static void main(String[] args) {
    for (int count=0; count<5; count++) {
      System.out.println(count + " ");
    }
  }
}

[Execution result] 0 1 2 3 4

In addition, Equation 1, Equation 2, and Equation 3 described in () for for can be omitted respectively. If the conditional expression expression 2 is omitted, the condition is always determined to be true, resulting in an infinite loop.

Sample.java


public class Sample {
  public static void main(String[] args) {
    int count1 = 0;
    for (; count1<5; count1++) {    //Example of omitting Equation 1
      System.out.print(count1 + " ");
    }
    System.out.println();    //new line
    for (int count2=0; count2<5;) {    //Example of omitting Equation 3
      System.out.print(count2 + " ");
    }
  }
}

[Execution result] 0 1 2 3 4 0 1 2 3 4

4. Extended for statement

In the Java language, ** extended for statement ** is provided as a convenient for statement. This is used when all the elements of ** array ** and ** collection ** are fetched and processed in order, and the description is simplified compared to the for statement.

Sample.java


for (Variable declaration:Reference variable name) {
Process statement;
}

The extended for statement extracts the elements in order from the reference variable specified in (), and assigns the extracted elements to the variables declared in the variable declaration. Therefore, the data type ** of the variable declared in the variable declaration must match the ** type of each element ** of the reference variable. The extended for statement ends when all the elements have been extracted from the reference variable.

Sample.java


public class Sample {
  public static void main(String[] args) {
    //Array declaration
    char[] array = {'a', 'b', 'c', 'd', 'e'};
    //All elements of the array array are fetched in order and output
    for (char c : array) {    //When processing with an extended for statement
      System.out.print(c + " ");
    }
    System.out.println();    //new line
    //When processing with a for statement
    for (int count=0; count<array.length; count++) {
      System.out.print(count + " ");
    }
  }
}

[Execution result] a b c d e a b c d e

Next time on control statements.

Recommended Posts

☾ Java / Iterative statement and iterative control statement
Java control syntax
Java control syntax
Java and JavaScript
XXE and Java
Java for statement
Basics of java basics ② ~ if statement and switch statement ~
Java switch statement
java iterative processing
Java switch statement and break, Kotlin when expression ...
Java and Swift comparison (1) Source control / Scope / Variables
[Java] for statement, while statement
Java instruction execution statement
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] for statement / extended for statement
[Java] String comparison and && and ||
[Java] Control syntax notes
Java --Serialization and Deserialization
[Java] Arguments and parameters
(Memo) Java for statement
timedatectl and Java TimeZone
hash and each statement
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
Study Java # 2 (\ mark and operator)
Java version control on macOS
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
About for statement and if statement
Java version 8 and later features
[Java] Multi-thread processing --Exclusive control
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Basic statement for beginners
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Java and Iterator Part 1 External Iterator
Java if and switch statements
Java class definition and instantiation
Java programming basics practice-for statement
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
Java JPA: Separating transaction control and logic in J2SE environment
[Java] HashCode and equals overrides
Java version control with jenv
Java methods and method overloads
Java programming basics practice-switch statement
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
[Java] Summary of control syntax
C # and Java Overrides Story
Java abstract methods and classes
Java while and for statements