Last time, I wrote what I learned from conditional branching, if statements, and switch statements. I'm glad to hear your comments.
It's been less than a year since I joined the company, but I think this is also an experience. The themes we will discuss this time are *** repetitive statements and repetitive control statements ***.
*** Iterative statement *** is a statement that iterates. There are two types of repetitive statements. ・ While statement ・ For statement
Let's see what a while statement is in a java program. Format while (conditional expression) { processing; }
test1.java
class test1{
public static void main(String[] args){
int a=0; //I assigned 0 to the int type variable a
while(a<10){ //Repeats the processing of the while statement block while the value of variable a is less than 10.
System.out.println(a); //Outputs the value of variable a
a++; //a++Is a= a+Same as 1
}
}
}
See the comments after // what they are doing. The () after while is a conditional expression. While this conditional expression determines True Process in the block of while statement. In this case, the value of the variable a is output and 1 is added to that value.
Let's see what a do-while statement is in a java program. Format do { processing; } while (conditional expression)
test2.java
class test2{
public static void main(String[] args){
int a = 0; //I assigned 0 to the int type variable a.
do{
System.out.println(a); //Outputs the value of variable a
a++; //Increase the value of variable a by one
}while(a<10)//Determine if the value of variable a is less than 10
}
}
What is the difference between the while statement and the do-while statement this time? The difference is that the (conditional expression) comes last. If the conditional expression is behind, Process the block of the do-while statement first. After that, the conditional expression is judged. *** I want to process it once! *** Use this do-while statement when you want to write such a repeating statement.
The for statement is a little confusing with the while statement and do-while statement. Format for (Equation 1; Equation 2; Equation 3) { processing; }
test3.java
class test3{
public static void main(String[] args){
int a = 0; //Assign 0 to the int type variable a.
for(int i = 0; i<10; i++){
//The variable i of type int is 0. While the variable i is less than 10
//Process the block of the for statement.
System.out.println(a);
a++;
}
}
}
Equation 1 is int i = 0, Equation 2 is i <10, and Equation 3 is i ++. Expression 1 is called an "initialization expression" in books and sites, and expresses how many times a block of for statements is repeated. Contains the formula. Equation 2 sets the upper limit of Equation 1. And Equation 3 processes the block After that, I am changing the value of the variable defined in Equation 1.
Last but not least, the iterative statement is used when you want to write a statement that repeats processing. I often hear the terms "loop processing" and "infinite loop", but I think that "loop processing" means repeating processing, and "infinite loop" means continuing the same processing all the time. You can use while to create an infinite loop. (You can also use a for sentence ...)
I still have to study, but I will continue to write articles. The most important thing to say in this series is that you can talk about the program so far *** even if you are inexperienced and continue whatever happens.
Let's do our best and continue! !!
Next time, I would like to talk about repetitive control that I did not use this time, and python that I am doing by myself.
Thank you for reading this far.
Recommended Posts