Java review ③ (control syntax [if / switch / while / for], conditional expression)

Part 1 "Control syntax"

  1. Three representative control structures ① Sequentially (executed in order from top to bottom) ② Branch ③ Repeat
  2. Branch if statement if ( age < 18) { // Processing }else if ( age == 20 ) { //処理 } else { //処理 }
  3. Repeat (loop) while( age > 18 ) {             }
  4. 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

  1. Confirmation of match, <= etc. is not allowed.
  2. 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.
  3. 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.
  4. for statement ① How to write a for sentence for (int i = 0; i <5; I ++) {// processing} The above sentence repeats the process 5 times.
  1. break statement Get out of the repeating block 2.continue End the current iteration and proceed to the next lap

Recommended Posts

Java review ③ (control syntax [if / switch / while / for], conditional expression)
Java control syntax
Java control syntax
[Java] for statement, while statement
java learning (conditional expression)
[Java] Control syntax notes
Review java8 ~ Lambda expression ~
Java if and switch statements
[Java] Summary of control syntax
Java while and for statements
New grammar for Java 12 Switch statements
Try an If expression in Java
Java study # 4 (conditional branching / if statement)
[Introduction to Java] Conditional branching (if statement, if-else statement, else if statement, ternary operator, switch statement)
Java12 came out, so I tried the switch expression for the time being
Java and Swift comparison (2) Basic type / arithmetic expression / control syntax / function definition
[Ruby] if statement concept of conditional expression
Java, for statement / while statement starting from beginner
Basics of java basics ② ~ if statement and switch statement ~