Java conditional branch

Introduction

It is still difficult to explain the small difficult parts in sentences, so I will write the basics of what I can do first.

How to write a conditional branch

kane.java


if(Conditional expression){
processing;
  }

** This is the basic form ** Boolean value, comparison operator (==, <Etc.), logical operators (etc.)&&, ||, !) Etc. to write the conditional expression.

block{}

Use when you want to combine multiple processes. No semicolon is required after ** {}. ** **

kane.java



int x = 40

if (x >= 30) {
   System.out.println("x is 30 or more.");
} else if (x > 20) {
   System.out.println("x is greater than 20 and less than 30.");
} else {
   System.out.println("x is less than 20.");
}

//This time it is executed in the first process

You can arrange else if as many as you want, but ** only the first matching condition is executed. ** **

switch statement

When the value of the condition matches the value of case, the process is executed. Note that it is easy to forget the ** colon (:) after the case. ** **

kane.java


switch(Condition value) {
case value 1:
processing;
      break;
case value 2:
processing;
      break;
case value 3:
processing;
      break;
}

** When the condition value in () and the case value are "==", the process is executed. **

kane.java


int = x

switch(x % 2) { //Condition value

  case 0: //true
      System.out.println("This is an even number"); //This runs
      break;
  case 1:
      System.out.println("This is odd");
      break;
}

break Break is extremely important in switch statements. ** Without break **, ** All executed **, break is ** when using switch statements **. ** * If you want to continue processing the case statement, omit break. There is a fall-through, and it is written by omitting the break of the case statement, but at this stage I do not understand how to use it and write it as a word. **

default Similar to else in an if statement.

kane.java


int number = -1;

 switch(number) {
  case 0:
      System.out.println("0");
      break;
  case 1:
      System.out.println("Is 1");
      break;
  case 2:
      System.out.println("2");
      break;
  default:
      System.out.println("I'm sorry. Once again thank you");
      break;
 }

String type can also be used

kane.java


String number = "zero";

 switch(number) {
  case "zero":
      System.out.println("0");
      break;
  case "Ichi":
      System.out.println("Is 1");
      break;
  case "D":
      System.out.println("2");
      break;
  default:
      System.out.println("I'm sorry. Once again thank you");
      break;
 }

It can be used in the same way as a numerical value.

If the String type is empty, "null" (A state where there is an irregularity of null other than the state of number = "", number = "null", etc.) In this case, please note that it will not be picked up by default. ** * I don't understand this either, but leave it as a memo **

At the end

Looking at other posts, I noticed that it becomes easier to see just by changing the indentation of {} and if statement switch statement. You should also be aware of the number of #s in the table of contents of the post.

Recommended Posts

Java conditional branch
java conditional branch
[Java] Conditional branch
Ruby conditional branch processing
java learning (conditional expression)
[Java] Branch and repeat
Swift conditional branch statement summary
Java study # 7 (branch syntax type)
Java
java (conditional branching and repetition)
Java
Conditional branch with helper method
[Ruby] conditional branch if statement
[Java] Branch enum with switch statement
Java study # 4 (conditional branching / if statement)
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Personal summary of conditional statements (basic)
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java starting from beginners, logical operators / conditional operators