Honestly I can't understand how to write a class at all (irrelevant to this content)
Iterative processing is a process that automatically repeats a certain process ** while ** statement and ** for ** statement
kane.java
while(conditions){
Repeated processing;
}
for(conditions){
Repeated processing;
}
kane.java
int = 1; //Initialization
while(i <= 5){ //conditions
System.out.println(i); //Repeated processing
i++; //Variable update that adds 1
}
for(int i = 1; i <= 5; i++){ //Initialization, conditions, updates can be written together
System.out.println(i); //Repeated processing
}
If you forget to add ** 1 to the variable **, the variable remains ** 1 **, and the condition becomes ** forever true **, so the iteration process is performed infinitely (infinite loop).
break In order to end the repetition, there is a method of forcibly ending ** using ** break other than setting the ** 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 it reaches 6
break;
}
System.out.println(i);
}
continue continue can skip only the processing of that lap and execute the next lap. It is common to use continue in combination with if statements.
kane.java
for(int i = 1; i <= 10; i++){
if (i % 3 == 0){ //Skip when it is a multiple of 3 and perform the next process
continue;
}
System.out.println(i);
}
console
1
2
4
5
7
8
10
** Multiples of 3 are skipped **
28 years old who notices the effect of coffee now.
Recommended Posts