[Java] Java iteration

1 minute read

Introduction

Honestly I can’t understand how to write a class at all (regardless of the content)

iteration

Repeat process is a process that automatically repeats a certain process while statement and for statement

kane.java


while (condition){
 Repeating process;
}

for (condition){
 Repeating process;
}

kane.java


int = 1; // initialization

while(i <= 5){ // condition
 System.out.println(i); // Repeat process
 i++; // add 1 update variables
}

for(int i = 1; i <= 5; i++){ // write initialization, condition, update together
 System.out.println(i); // Repeat process
}

If you forget to add 1 to the variable, the variable will remain **1 and the condition will be forever forever, so the iteration will be performed indefinitely (infinite loop).

break

In order to end the iteration, there is a method to forcibly end ** using break, in addition to setting **condition to false. By combining with conditional branching such as if statement, iterative processing can be ended at any place.

kane.java


for(int i = 1; i <= 10; i++){
 if (i> 5){ //Forced termination when 6
  break;
 }
 System.out.println(i);
}

continue

continue can skip the processing for that lap and execute the next lap. It is common to use continue in combination with an if statement.

kane.java


for(int i = 1; i <= 10; i++){
 if (i %3 == 0){ // Skips when it is a multiple of /3 and performs the next processing
  continue;
 }
 System.out.println(i);
}

console


1
2
Four
Five
7
8
Ten 

Multiples of 3 skipped

At the end

I’m 28 years old when I realize the effect of coffee.

Tags:

Updated: