Java review ③ (control syntax [if / switch / while / for], conditional expression)
Part 1 "Control syntax"
- Three representative control structures
① Sequentially (executed in order from top to bottom)
② Branch
③ Repeat
- Branch
if statement
if ( age < 18) {
// Processing
}else if ( age == 20 ) {
//処理
} else {
//処理
}
- Repeat (loop)
while( age > 18 ) {
}
- Scope
while( age > 18 ){
int a;
a = 10;
}
Since the variable a in the while statement is declared in the while statement, it cannot be used outside the while block.
The range in which the variable a can be used in this block is called the "scope".
Part 2 "Conditional expression"
(1) The inside of the parentheses of if and while must be a conditional expression.
② Use relational operators (== <=> =! = Etc.)
(3) The conditional expression turns into a boolean value after evaluation.
④ Character string comparison is special
if (string.equal (“ramen”)) {}
⑤ Logical operator( && || )
if ( num == age && num > -1){ }
Part 3 "Various writing styles using conditional expressions"
1.switch
① witch sentence rule
- Confirmation of match, <= etc. is not allowed.
- Compare integers or letters.
② How to write a switch statement
switch ( age ) {
case 1;
//処理
break;
case 2:
//処理
break;
default:
//処理
}
③ Precautions for switch statement
If you forget to write break, the processing in the next case will be executed even if the cases do not match.
- do-while
① How to write do-while
do {
//処理
} While (conditional expression)
Unlike the while statement, the processing is performed first rather than the evaluation of the conditional expression.
Therefore, the difference from the while statement is that the process is always performed once.
- for statement
① How to write a for sentence
for (int i = 0; i <5; I ++) {// processing}
The above sentence repeats the process 5 times.
- The content of the for statement is divided into three areas separated by a semicolon.
It is not necessary to describe these three.
Part 4 "Convenient sentences that can be used in repeated sentences"
- break statement
Get out of the repeating block
2.continue
End the current iteration and proceed to the next lap