Basics of java basics ② ~ if statement and switch statement ~

・ If statement (1) If you use the if statement, you can express the sentence "If ..." in the program. Let's write a program that says, "If the probability of precipitation is 50% or more, bring an umbrella." (A fragment of the program to explain the if statement)

if (p >= 50) { System.out.println ("Bring an umbrella"); }

This works by "examining the value of the variable p and if the value is 50 or more," bring an umbrella "is displayed on the screen." The above program has the following structure.   if (conditional expression) { Processing when the condition is satisfied }

The value of the conditional expression is "true (true) when the condition is met" and "false (false) when the condition is not met". The value of the conditional expression will always be either of these. After the conditional expression enclosed in (), the process enclosed in {} comes. This indicates the range of processing to be performed when the conditions are met. The if statement checks whether the condition in () is satisfied, and if it is satisfied, it executes in {}, and if it is not satisfied, it does not execute in {}. (2) If ... else sentence can be used to express the sentence "If ... If ... Otherwise ..." in the program.    if (p >= 50) { System.out.println ("Bring an umbrella"); } else { System.out.println ("I don't bring an umbrella"); }   The new word that appears here is "else". It is exactly equivalent to the Japanese word "otherwise". There is also a {} after the else. Here, the processing to be performed when the condition is not satisfied (not satisfied) is described.   if (conditional expression) { Processing when the condition is satisfied (true) A } else { Processing when the condition is not satisfied (false) B }

It does not mean that the condition is met and not met at the same time. Therefore, either process A or process B is always performed. ③ Operator "&&"" Represents "~ katsu ~", and the operator "||"" Represents "~ or ~".    0 <= n && n < 50   Is "n is greater than or equal to 0 and n is less than 50". "Conditional expression 1 && conditional expression 2" is a condition that is "true only when both conditional expression 1 and conditional expression 2 are satisfied". next,    n < 0 || 100 < n

Is "n is less than 0 or n is greater than 100". "Conditional expression 1||"Conditional expression 2" is a condition that is "true when at least one of conditional expression 1 and conditional expression 2 is satisfied". ③ chain of if statements (else if) if (conditional expression 1) { When conditional expression 1 is satisfied } else if (conditional expression 2) { Conditional expression 1 is not satisfied When conditional expression 2 is satisfied } else { When both conditional expressions 1 and 2 are not met } You can choose between if and else by inserting else if. This is because one of the three will always be executed. Even if it is common sense for human beings, do not expect common sense from computers. Everything must be written properly and explicitly.

・ Switch statement (1) The syntax for selecting and executing one from many options is called a "switch statement".   switch (expression) { case Constant expression 1: Process 1       break;

case Constant expression 2: Process 2       break;

default: Process 3       break; }

The range enclosed in {} is the candidates for selection. At the beginning of each candidate, write "case" for each candidate. Write a colon (:) at the end of the case. Write "default" as the last candidate. This means "if it wasn't one of the cases I've given one by one". It can be said that the default of the switch statement corresponds to the else of the if statement. You can write as many cases as you like in a switch statement, but you can only write one default. If the value is not explicitly stated as a case, the following will be executed under default. (2) What is written between case and case (or default) is the processing when it is selected. Write "break;" at the end of the process. This means "end the process here". If you write multiple cases without break, the same process will be performed. In other words, don't forget to write the required break. Otherwise, the process will proceed to other cases. (3) When executing the switch statement, the formula enclosed in () is calculated first. This is called "expression evaluation". If the result of evaluating the expression (calculation result) is equal to the value of constant expression 1, process 1 is executed, and if it is equal to the value of constant expression 2, process 2 is executed. If the values ​​of both constant expression 1 and constant expression 2 are not equal, the process 3 corresponding to default is executed. A switch statement is a syntax that selects processing according to the value of an expression. Therefore, do not write the same constant expression in more than one case in one switch statement. If there are two or more of the same constant expression, it will be difficult to know which process to execute.

Recommended Posts

Basics of java basics ② ~ if statement and switch statement ~
Java if and switch statements
Java switch statement
Java, if statement / switch statement starting from beginner
Basics of try-with-resources statement
Java switch statement and break, Kotlin when expression ...
Basics of threads and Callable in Java [Beginner]
About for statement and if statement
Omission of curly braces in if statement (Java silver)
Basics of character operation (java)
Java programming basics practice-for statement
☾ Java / Iterative statement and iterative control statement
Summary of Java language basics
I summarized the types and basics of Java exceptions
Advantages and disadvantages of Java
I tried to summarize the basics of kotlin and java
Basics of conditional branching and return
About fastqc of Biocontainers and Java
[Java] Really scary switch statement story
[Java] Judgment of identity and equivalence
Java study # 4 (conditional branching / if statement)
About if statement and branch processing
switch statement
Java basics
Collection of programming selection tasks to make and remember (Java basics)
Java basics
Java basics
if statement
Memorandum of new graduate SES [Java basics]
After 3 months of Java and Spring training
[# 1 Java] Basics of Java-Major premise before studying-
[Java] Inheritance and structure of HttpServlet class
[day: 5] I summarized the basics of Java
[Ruby] if statement concept of conditional expression
Looking back on the basics of Java
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Discrimination of Enums in Java 7 and above
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
Basics of Java development ~ How to write a program (flow and conditional branching) ~
Java Development Basics ~ Development Environment Settings and Project Creation ~
[Java] Personal summary of classes and methods (basic)
[Java] use Optional instead of if (hoge! = null) {...}
java programming basics
progate java L1 basic summary console.log, difference between variables and constants, if statement
I compared the characteristics of Java and .NET
switch and EnumMap
Ruby on Rails ~ Basics of MVC and Router ~
Java JAR basics
JAVA: Realizes generation and scanning of various barcodes
Object-oriented (Java) basics
Switch between multiple versions of Java on Mac
Java and JavaScript
XXE and Java
Java concurrency basics
[Introduction to Java] Basics of java arithmetic (for beginners)
Java for statement
[Java] Types of comments and how to write them
Summary of ToString behavior with Java and Groovy annotations
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~