・ 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