Java if and switch statements

Conditional branch

It is a process that is performed only in a specific situation. If it's OO, it's like XX. For example, if the weather forecast is rainy, bring an umbrella. For if statement

Main.java


  if (Conditional expression) {
processing;
}

Applying to the previous example

Main.java


if (weather forecast==rain) {
Bring an umbrella;
}

It will be. Apply an if statement to a number. If true

Main.java


int x = 10;
  if (x == 10){
    System.out.println("x is 10");
  }

In the above case, the conditional branch of (x == 10) will be true, so the console will display ** x as 10 **. If false

Main.java


int x = 20;
  if (x == 10){
    System.out.println("x is 10");
  }

In the above case, the conditional branch of (x == 10) will be false, so nothing will be displayed on the console. else The else of the if statement can be conditional branching "If ~, OO, otherwise XX".

Main.java


int x = 10;
   if (x < 20){
     System.out.println("x is less than 20");
   } else {
     System.out.println("x is greater than 20");
   }

In the above case, the result will be displayed as ** x is less than 20 **.

Main.java


int x = 30;
   if (x < 20){
     System.out.println("x is less than 20");
   } else {
     System.out.println("x is greater than 20");
   }

In the above case, the result will be displayed as ** x is greater than 20 **. else if By combining if, else if, and else, you can create a conditional branch such as "If ~, then ◯◯, if not, then △△, if neither, □□".

Main.java


int x = 25;
   if (x < 30){
     System.out.println("x is greater than 30");
   } else if {
     System.out.println("x is greater than 20 and less than 30");
   } else {
     System.out.println("x is less than 20");
  

In the above case, the result will be ** x is greater than 20 and less than 30 **. One thing to note is that even if multiple conditions are met, only the first one that is met will be executed.

switch statement

Conditional branching also has a syntax called a switch statement. The switch statement is executed when the condition value matches the case value.

Main.java


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

Write as above. After the case is a colon (:). break; is an instruction to end the switch statement. Without break ;, after processing the matching case, the processing of the next case will also be executed. Example of actual code

Main.java


int x=10
swich(x % 2) {
  case 0:
    System.out.println("Even")
    break;
  case 1:
    System.out.println("Odd")
    break;
}

In the above example, ** even ** is executed. default Use default for the process to be executed when it does not match any case. [Example]

Main.java


swich(rank) {
  case 1:
    System.out.println("First place")
    break;
  case 2:
    System.out.println("2nd place")
    break;
  case 2:
    System.out.println("3rd place")
    break;
  default:
    System.out.println("4th or lower")
    break;
}

In the above case, if it is 4th or lower, default will be executed.

Recommended Posts

Java if and switch statements
Basics of java basics ② ~ if statement and switch statement ~
Java while and for statements
[Java] Reduce if statements with Enum
Java conditional branching: How to create and study switch statements
Java, if statement / switch statement starting from beginner
switch and EnumMap
Java and JavaScript
XXE and Java
Java switch statement
Java switch statement and break, Kotlin when expression ...
Getters and setters (Java)
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Ruby Learning # 17 If Statements
Switch java with direnv
Java --Serialization and Deserialization
[Java] Arguments and parameters
Switch between JDK 7 and JDK 8
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
Study Java # 2 (\ mark and operator)
About for statement and if statement
Java version 8 and later features
[Java] Difference between == and equals
[Java] Stack area and static area
[Java] Generics classes and generics methods
Java programming (variables and data)
Java encryption and decryption PDF
Ruby Learning # 18 If Statements (Con't)
About Java variable declaration statements
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
[Java] HashCode and equals overrides
[Java Silver] Things to be aware of regarding switch statements
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
[Java] Summary of for statements
Java review ③ (control syntax [if / switch / while / for], conditional expression)
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
Seasonal display with Java switch
[Java] Upload images and base64
C # and Java Overrides Story
Java abstract methods and classes
Java encapsulation and getters and setters
Evolutions of enums and switch statements! ?? Try to achieve algebraic data types and pattern matching in Java
Utilization of Java array elements, for, length, value, and extended for statements
About Java static and non-static methods
I compared PHP and Java constructors
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable