[Java] Personal summary of conditional statements (basic)

[Java] Personal summary of conditional statements (basic)

This is the engineer t-77. I personally summarized what I thought was important in studying Java. This time, it's about conditional statements.

① if statement

--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);

② switch statement

--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;
}

③ while statement

--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++;
}

④ do ~ while statement

--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) ;

⑤ Sentence nesting

--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.");
    }
  }
}

⑥ break statement

--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.

⑦ continue statement

--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

[Java] Personal summary of conditional statements (basic)
[Java] Personal summary of classes and methods (basic)
[Java] Summary of for statements
Java static [Personal summary]
Personal summary about Java
Summary of Java support 2018
[Java] Basic summary of Java not covered by Progate ~ Part 1 ~
Basic knowledge of SQL statements
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
Summary of Java language basics
Summary of Java Math class
Summary of basic functions of ImageJ
[Java] Summary of control syntax
Summary of java error processing
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java] Basic summary of Java not covered by Progate ~ Part 2 · List ~
Summary of [Java silver study] package
Basic usage of java Optional Part 1
Basic processing flow of java Stream
Summary of object-oriented programming using Java
[Basic knowledge of Java] Scope of variables
Basic structure of Java source code
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Summary of changes other than JEP of Java10
Basic knowledge of Java development Note writing
[Java] [Maven3] Summary of how to use Maven3
[Basic knowledge of Java] About type conversion
Java Summary of frequently searched type conversions
Summary of Java Math.random and import (Calendar)
Docker basic summary
Java knowledge summary
[java] Summary of how to handle character strings
Summary of Java environment settings for myself [mac]
[Docker] Introduction to docker compose Basic summary of docker-compose.yml
Java conditional branch
Summary of basic knowledge of Rails acquired by progate
Java basic grammar
[Java] Summary of how to abbreviate lambda expressions
Java basic grammar
Java related summary
Java basic knowledge 1
[Java] Basic structure
[Java] [Basic] Glossary
Java 8 documentation summary
Java basic grammar
Java basic grammar
Java 11 document summary
[Java] Overview of Java
Java exercises [Basic]
[Java] Conditional branch
Java review ③ (Basic usage of arrays / reference type)
This is convenient! Summary of popular Java library + α
Summary of ToString behavior with Java and Groovy annotations
[Java Silver] Summary of points related to lambda expressions
Summary of revisions (new era) support by Java version
[java.io] Summary of Java string input (InputStream, Reader, Scanner)
Summary of knowledge required to pass Java SE8 Silver