[Note] What I learned in half a year from inexperienced (Java)

What I learned in half a year from inexperienced (2)

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.

Conditional branch

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

Let's write conditional branch in java

The above has provided an overview. Let's actually write a program of if statement and switch statement using java.

if statement

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

Write the process when conditional expression 1 is True

} else if (conditional expression 2) {

Write the process when conditional expression 2 is True

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

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:

Write the process when the value is 1.

break; case value 2:

Write the process when the value is 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.

Summary

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

[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned when building a server in Java
What I learned from Java monetary calculation
What I learned in Java (Part 2) What are variables?
What I learned in Java (Part 3) Instruction execution statement
What an inexperienced engineer who took a leave of absence from college learned in 2020
What I learned in Java (Part 4) Conditional branching and repetition
What I pointed out as a reviewer for half a year
I created a PDF in Java.
What I learned with Java Gold
What I learned with Java Silver
What I have learned in Java (Part 1) Java development flow and overview
What I learned from doing Java work with Visual Studio Code
What I investigated in Wagby development Note 1
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
A note of what I stumbled upon and noticed in catching up with Laravel from Rails
[Rails] What I learned from a little stumbling block when using ancestry
[JAVA] Project Euler, I got stuck in Q8, so make a note
What I don't like when using interface of a function with default arguments in Kotlin from Java
What i learned
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
I tried hitting a Java method from ABCL
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a rock-paper-scissors game in Java (CLI)
A quick review of Java learned in class
I wrote a prime factorization program in Java
Summary of what I learned in Spring Batch
I made a simple calculation problem game in Java
A quick review of Java learned in class part4
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to create a Clova skill in Java
What I learned ② ~ Mock ~
A note for Initializing Fields in the Java tutorial
I tried to make a login function in Java
What I learned ① ~ DJUnit ~
[Note] Create a java environment from scratch with docker
A quick review of Java learned in class part3
I made a Wrapper that calls KNP from Java
[Rilas] What I learned in implementing the pagination function.
A quick review of Java learned in class part2
Call a program written in Swift from Processing (Java)
I tried to find out what changed in Java 9
How a liberal arts engineer passed Java Silver in half a year after joining the company
I tried to make an application in 3 months from inexperienced
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Easily get an integer from a system property in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
Get a non-empty collection from an Optional stream in java
A note about Java GC
What I researched about Java 8