At the company I am learning Java. My boss ordered me to write concise and easy-to-understand sentences in writing. With that in mind, I will write this time as well.
A conditional branch is a statement whose processing is determined by whether or not the condition is met. There are two types of statements in conditional branching.
--if statement
The if statement is a statement that performs different processing when True and False for one condition. For example, consider two cases, True and False, under the condition of "If it is sunny tomorrow", such as "If it is sunny tomorrow, I will take a walk" and "If it is not sunny tomorrow, I will not take a walk".
--switch statement
A switch statement is a statement that has processing for multiple conditions. Different processing is performed depending on what the value specified in that condition is. For example, "Tomorrow's weather", "If it's sunny, I'll take a walk", "If it's cloudy, I'm at home", "Otherwise, I'll do nothing", and so on. The switch statement is used when you want to perform different processing for "cloudy" and "others (other than sunny and cloudy)".
The above has provided an overview. Let's actually write a program of if statement and switch statement using java.
test1.java
class test1{
public static void main(String[] args){
int a = 10;
if(a > 7){
System.out.println("Greater than 7");
}else if(a > 5 && a <= 7){
System.out.println("Greater than 5 and less than 7");
}else{
System.out.println("Other than that");
}
}
}
In the case of if statements, there are three types: if statements, if-else statements, and statements using else if. This program uses else if. The usage is as follows. if (conditional expression 1) {
} else if (conditional expression 2) {
}else{ Write the process for #False } ・ Conditional expression 1 is (a> 7). In this case, it means that the int type variable a is larger than 7. ・ Conditional expression 2 is (a> 5 && a <= 7). Conditional expression 2 uses the logical operator "&&". This does not perform the process of "# Write the process when conditional expression 2 is True" unless both the conditional expression "a> 5" and the conditional expression "a <= 7" are True. There is also a conditional expression "a <= 7". If "a <7", then "the value of variable a is less than 7." However, if "a <= 7", then "the value of variable a is 7 or less."
This conditional expression is also used in the following switch statement.
test2.java
class test2{
public static void main(String[] args){
int a = 10;
switch(a){
case 5:
System.out.println("5");
break;
case 10:
System.out.println("10");
break;
default:
System.out.println("Neither 5 nor 10");
break;
}
}
}
The way to write a switch statement is as follows. switch (condition) { case value 1:
break; case value 2:
break; default: Write the process for #default break; } What I want to pay attention to here is that the "condition" and the values 1 and 2 must be of the same type. In this program, the variable a was of type int. Therefore, both case values 1 and 2 are int type. Another thing I would like to pay attention to is default. default describes what happens when the value is other than 1 and 2. This default may or may not be present.
This time, I introduced the features of each so that you can understand the difference between conditional branching, if statement, and switch statement. I wrote it for my own learning, but if you have any suggestions, please comment. I will correct it even if it is difficult to understand here.
It was a boring blog, but this time I summarized the conditional branching. Next time, we will prepare outputs for repetitive statements and repetitive control statements.
Thank you for reading.
Recommended Posts