I'm not good at it forever.
Iterative processing is also called ** loop **, and there are roughly three types of iterative statements: ** while statement **, ** do-while statement **, and ** for 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.
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.
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
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