java learning day5

Classification of flow control statements
1. Sequence structure 
The code written first is executed first, and the code written later is executed
2. Selection structure
Also known as branch structure
if statement
a, if (judgment expression) {statement 1}, if the judgment expression If it is true, then execute statement 1. If the result is false, then do not execute it, which is equivalent to ending if statement
b, if (judgment expression) {statement 1}
   else{statement 2}
If the judgment expression is true, then execute statement 1 ,
If the judgment expression is false, then execute statement 2.
c, if (judgment expression 1){statement 1}
   else if (judgment expression 2){statement 2}
   ...
   else {statement n+1}
judge first Expression 1, if it is true, then execute statement 1, if it is false, judge expression 2, if it is true, then execute statement 2, if it is false, continue to judge the expression after the next else if, if all expressions are If false, execute the statement n+1 after the else
========================================= =====================
switch


3, loop structure
switch(variable){
case value 1:
     statement 1;
     break;
case value 2;
     statement 2;
     break;
...
case value n;
     statement n;
     break;
default:
     statement n+1;
     break;
}
Principle: compare the value of the variable with the value after the case, if they are the same, execute the statement after the value, if all If the values ​​are different, execute the statement under default.
Note : The data type of the variable must be four types: byte short int char
. After jdk1.5, it can be an enumeration type.
After jdk1.7, it can be
the value after the String case: the The value cannot be repeated, the value must be a constant, and the constant type of the value must be consistent with the data type of the variable.
Conditions for the end of switch execution:
1. Execute to curly brackets
2. When encountering a break
case, it is equivalent to a menu option

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325824970&siteId=291194637