Java control syntax

Learning log

The order in which statements are executed is called a control structure, and there are mainly "** sequential ", " branch ", and " repeat **".

Control syntax components

[Conditional expression] An expression showing a branch condition or a condition to continue repeating [Block] A set of statements to be executed by branching or repeating

python


・ Branch syntax
if(Conditional expression) {
block
} else {
block
}

-Repeat syntax
while(Conditional expression){
block
}  

Branch syntax variations

・ Three types of if syntax

python


▪︎if-else syntax(Uninflected word)
//Example
if(age >= 20) {
  canDrink = true;  //If the condition is true, the processing of this block works.
} else {
  canDrink = false; //If the condition is false, the processing of this block works.
}

▪︎ if only syntax
//Example
if( age >= 20) {
  canDrink = true;
}

▪︎if-else if-else syntax
//Example
if( height >= 170) {
  size = 'L'; 
} else if(height >= 155){
  size = 'M';
} else if(height >= 140){
  size = 'S';
} else {
  size = '?';
}

switch syntax

Conditions that can be rewritten as a switch statement (1) All conditional expressions can only be used to compare whether the left and right sides match, such as "variable == value" and "variable == variable" (<,>,! =, Etc. cannot be used) (2) The value to be compared is an integer (byte type, short type, int type), character string (String type) or character (char type), not a minority or boolean value.

python


//Example
switch(fortune){
  case 1:
    System.out.println("Daikichi");
    break;
  case 2:
    System.out.println("Nakayoshi");
    break;
  case 3:
    System.out.println("Kichi");
    break;
  default:  //What to do if none of the cases apply(Can be omitted if not needed)
    System.out.println("Bad");
}

-Since the switch statement only jumps to the applicable case, if break is not described, the program will be read in sequence after jumping to a specific case.

Iterative syntax variations

・ Two types of while statements

python


▪︎ while syntax(Uninflected word)
//Example
while(temp > 25) {
  temp--;
  System.out.println("I lowered the temperature by 1 degree");
}

▪︎do-while syntax
//Example
do{
  temp--;
  System.out.println("I lowered the temperature by 1 degree");
} while(temp > 25);

・ Repeat by for statement

python


for (Unter variable initialization process;Repeat condition;Increase / decrease processing of counter variables) {
Repeated processing
}
//Example
for( int i = 0; i < 10; i++) {
  System.out.println(i);
}
//The variable i is initialized and 0 is assigned. Process in the block until i is greater than 10.
//I every time processing++(Add 1 to the variable i)The process is performed.

Control structure nesting

Such multiple structures such as "branch in branch" and "branch in repetition" are called "** nested " and " nested **".

python


▪︎ Branch processing in branch processing
//Example
if(height > 170){
  if(eye > 1.0){
    System.out.println("Pass");
  }
}

▪︎ Branch processing in repetition
//Example
do{
  if(i % # == 0){
    System.out.println(i);
  }
  i++
} while(i < 100);

・ Break statement (interrupts the repetition itself)

python


for(int i = 1; i < 10; i++) {
  if(i == 3){
    break;
  }
  System.out.println(i);
}

・ Continue statement (interrupt only this sentence and go to the next lap)

python


for(int i = 1; i < 10; i++) {
  if(i == 3){
    continue;
  }
  System.out.println(i);
}

Recommended Posts

Java control syntax
Java control syntax
[Java] Control syntax notes
[Java] Summary of control syntax
Control syntax operator
Organized memo in the head (Java --Control syntax)
Java version control on macOS
Java basic syntax + α trap
[Java] Multi-thread processing --Exclusive control
Version control Java with SDKMAN
I touched Scala ~ [Control syntax] ~
Java study # 7 (branch syntax type)
Java version control with jenv
Java
☾ Java / Iterative statement and iterative control statement
Java
Java review ③ (control syntax [if / switch / while / for], conditional expression)
Syntax examples often used in Java
[Java] Object-oriented syntax --class method / argument
[ev3 × Java] Display, sound, LED control
Notes on signal control in Java
[ev3 x Java] Single motor control
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review