This is the engineer t-77. I personally summarized what I thought was important in studying Java. This time, it's about conditional statements.
--The if statement processes the statement in the block if the condition is true. --By using else, the conditions other than the set conditions are judged as true, and the statements in that block are processed. --Multiple conditions can be set by using else if.
sample.java
//The output words differ depending on the value of number.
// number ==If it is 10, the process is executed.
if (number == 10) {
System.out.println("Wayne");
// number ==In case of 11, the process is executed.
} else if (number == 11) {
System.out.println("Ryan");
//If number is other than the above, processing is executed.
} else {
System.out.println("other");
}
In the case of the above example sentence, "output processing" is common in the if statement. Therefore, it is easier to correct and maintain by summarizing "output processing" as shown in the example sentence below.
sample2.java
//The output words differ depending on the value of number.
//Define the variable name.
String name;
if (number == 10) {
name = "Wayne";
} else if (number == 11) {
name = "Ryan";
} else {
name = "other";
}
//The character string assigned to name by number is output.
System.out.println(name);
--The switch statement processes the statement if it matches the value of case. --If none of them match, process the default statement. --default can be omitted. --Be sure to write "break" to end the process.
sample.java
//The output words differ depending on the value of number.
//If the value of case is 10, the process is executed.
switch (number) {
case 10:
System.out.println("Wayne");
break;
//If the value of case is 11, the process is executed.
case 11:
System.out.println("Ryan");
break;
//If the value is other than the above, the default process is executed.
default:
System.out.println("other");
break;
}
As shown below, it is possible to perform the same processing in multiple cases.
sample2.java
//The output words differ depending on the value of number.
//If the value of case is 1 or 2, the process is executed.
switch (number) {
case 1:
case 2:
System.out.println("Low rating");
break;
//If the value of case is 3, the process is executed.
case 3:
System.out.println("And points");
break;
//If the value of case is 4 or 5, the process is executed.
case 4:
case 5:
System.out.println("Highly rated");
break;
//If the value is other than the above, the default process is executed.
default:
System.out.println("No evaluation");
break;
}
--The while statement iterates through the statements in the block until the condition becomes false. Note that if you do not describe the condition so that it becomes --false, the block will loop forever.
sample.java
//Set the initial value.
int i = 1;
//Iterate over the statement until false.
while (i <= 11) {
System.out.println("The uniform number is" + i + "It's your turn");
//By increasing i, it will not loop forever.
i++;
}
--The do-while statement iterates the statements in the block until the condition becomes false. --If the condition is false before the first iteration of the while statement, no processing is performed, whereas the do-while statement iterates at least once and then iterates until the condition becomes false. I do.
sample.java
//Set the initial value.
int i = 1;
//Do Process statements in blocks.
do {
System.out.println("The uniform number is" + i + "It's your turn");
//By increasing i, it will not loop forever.
i++;
//Iterate over the statement until false.
} while (i <= 11) ;
--Sentences can be nested (nested). --By nesting, you can make more complicated descriptions. ――If you nest too much, the code will be difficult to see, so when developing in collaboration with multiple people, it is better to separate the parts that can be separated.
sample.java
//It is possible to describe like a for statement in a for statement.
//It is also possible to combine different conditional statements such as if statements.
for (int i = 1; i <= 11; i++) {
System.out.println("The uniform number is" + i + "It's the turn player.");
for (int s = 1; s <= 5; s++) {
System.out.println(s + "I hit the second shot.");
if (s == 3) {
System.out.println("I decided to shoot.");
}
}
}
--The break statement can force the process flow to end and exit the block. --Break is required in the switch statement.
sample.java
// i =If it is 8, the process ends.
for (int i = 1; i <= 11; i++) {
System.out.println("The uniform number is" + i + "It's the turn player.");
if (i == 8) {
System.out.println("The process ends.");
break;
}
}
console
The player with the number one.
The player with the second uniform number.
The player with the number 3 is.
The player with the number 4 is.
The player with the number 5 is.
The player with a uniform number of 6.
The player with a uniform number of 7.
The process ends.
※i =When it reaches 8, the process is terminated by break.
--The continue statement can skip the process in the iteration, return to the beginning of the block, and start the next process.
sample.java
// i =In case of 8, the process is skipped and the next process is started.
for (int i = 1; i <= 11; i++) {
System.out.println("The uniform number is" + i + "It's the turn player.");
if (i == 8) {
continue;
}
}
console
The player with the number one.
The player with the second uniform number.
The player with the number 3 is.
The player with the number 4 is.
The player with the number 5 is.
The player with a uniform number of 6.
The player with a uniform number of 7.
The player with a uniform number of 9.
The player with a uniform number of 10.
The player with a uniform number of 11.
※i =When it reaches 8, the process is skipped by continue.
It was part of my Java study, but conditional statements are also used in other languages, such as JavaScript, so it was a good opportunity to revisit the basics. That's it.
Recommended Posts