[Java ~ Conditional branching / Iterative processing ~] Study memo (3)

Learning Java. I will make a note so that I can look back on it for review. It's almost my study memo. Please do not excessive expectations.

Conditional branch

--if statement

The if statement means "if-if" in English, and the part corresponding to this "-" is the part in () after the if. This is called a conditional expression. If the conditional expression is true, the processing inside {} is performed, and if the conditional expression is false, nothing is done.

Conditional expressions include "true / false value" and "comparison operator (==, <Etc.) ”,“ Logical operators (&&, ||, !) ”Is utilized. No semicolon is required after {}.

How to write


if(Conditional expression){
  //Processing content;
}

Example


//Definition of variables of integer type
int x = 5;
//Using an if statement, output "x is greater than 2" when the variable x is greater than 2.
if(x > 2) {
  System.out.println("x is greater than 2");
}

Output result


x is greater than 2

-else、 else if

else means "other" in English, and if you combine if and else, Conditional branching of "If ..., then ◯◯, otherwise △△" is possible.

Furthermore, by using else if, further conditional branching becomes possible. By combining if, else if, and else, It is possible to realize a conditional branch such as "If ... then ◯◯, if not XX then △△, if neither is □□".

Example


//Definition of variables of integer type
int number = 12;
    
//Conditional branching that combines if, else if, and else
if (number < 10) {
  System.out.println("Less than 10");
}else if(number < 20){
  System.out.println("10 or more, less than 20");
}else{
  System.out.println("over 20");
}

Output result


10 or more, less than 20

--switch statement

Conditional branching also has a syntax called a switch statement. The switch statement is processed when the condition value matches the case value. Also, break is an instruction to end the switch statement. If there is no break, after processing the matching case, the processing of the next case will also be executed. Unintended processing will occur.

Note that the colon (:) after the case is easy to forget.

How to write


switch(Condition value){
case value 1:
    //Processing content;
    break;
case value 2:
    //Processing content;
    break;
case value 3:
    //Processing content;
    break;
}

Example


int number = 12;
    
switch(number%3) {
  case 0:
    System.out.println("Dividable by 3");
    break;
  case 1:
    System.out.println("Divide by 3 to get 1 remainder");
    break;
  case 2:
    System.out.println("Divide by 3 to get 2");
    break;
}

Output result


Dividable by 3

In the switch statement, the process to be executed when it does not match any case can be specified in default. This is similar to the else of an if statement.

Example


int number = 18;
    
switch(number%5) {
  case 0:
    System.out.println("Dividable by 5");
    break;
  case 1:
    System.out.println("Divide by 5 to get 1 surplus");
    break;
  case 2:
    System.out.println("Divide by 5 to get 2");
    break;
  default:
    System.out.println("other than that");
    break;
}

Output result


other than that

Iterative processing

--while statement

while is an English word meaning "between", and the while statement is " repeat the process in {} while the condition is true " Can be done.

How to write


while(conditions){
  //Repeated processing;
}

--Processing flow of while statement

How to write


int number = 10; //①
    
//Create an iterative process that repeats when number is greater than 0 using a while statement
while(number > 0){ //②
  System.out.println(number); //③
  number-=1; //④
}

① Variable initialization ↓ ② Conditions ↓ ③ Repeated processing ↓ ④ Variable update ↓ ② Conditions ↓ ③ Repeated processing ↓ ④ Variable update

--for statement

The for statement is also one of the iterative processes. In the for statement, describe the three " variable initialization, conditional expression, variable update " in () after for. Separate each with a semicolon (;), but note that the last variable update does not have a semicolon (;).

How to write


//Create an iterative process that repeats when number is greater than 0 using a for statement
for(int number = 10; number > 0; number-=1){
  System.out.println(number);
}

-break、continue

To end the iterative process, use break instead of setting the condition to false. There is a way to forcibly terminate it.

Example


int i = 1;
//When i is a multiple of 5, iterative processing ends
while (i < 10) {
  if(i % 5 == 0){
    break;
  }
  System.out.println(i);
  i++;
}

Output result


1
2
3
4

continue can skip only the processing of that lap and execute the next lap.

Example


for (int j = 1; j < 10; j++) {
//Skip processing when j is a multiple of 3
  if(j%3==0){
    continue;
  }
  System.out.println(j);
}

Output result


1
2
4
5
7
8

Past posts

[Java ~ Variable definition, type conversion ~] Study memo [Java ~ False Value ~] Study Memo 2

Recommended Posts

[Java ~ Conditional branching / Iterative processing ~] Study memo (3)
Ruby study memo (conditional branching)
[Ruby ~ Iterative processing ~] Study memo 4
Java study # 4 (conditional branching / if statement)
java iterative processing
[Java ~ Method ~] Study memo (5)
[Java ~ Array ~] Study memo 4
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Java conditional branching: How to create and study switch statements
Memorandum (other conditional bifurcation, iterative processing)
[Study session memo] Java Day Tokyo 2017
[Java] Processing time measurement method memo
Java memo
Iterative processing
[Java ~ Variable definition, type conversion ~] Study memo
[Java ~ Classes and External Libraries ~] Study Memo (6)
java anything memo
Let's study Java
Java Silver memo
Java conditional branch
java, maven memo
[Ruby] Iterative processing
Java thread processing
Java SE 7 memo
java anything memo 2
Java string processing
[Java] Study notes
Java study memorandum
[Java] Multi-thread processing
Study Java Silver 1
Java specification memo
[Java] Conditional branch
[Java] Stream processing
Java pattern memo
Ruby conditional branch processing
Java development environment memo
java learning (conditional expression)
Java Silver Study Day 1
java basic knowledge memo
Java learning memo (method)
Java Kuche Day memo
Program using conditional branching
java se 8 programmer Ⅰ memo
Java paid private memo
Dot installation study memo 01
Java study # 1 (typical type)
Conditional branching of numbers
Java learning memo (basic)
java lambda expression memo
(Memo) Java for statement
My Study Note (Java)
JAVA constructor call processing
Java lambda expression [memo]
Java learning memo (interface)
Also complicated conditional branching
Java random, various processing
[Java] Implicit inheritance memo
Java learning memo (inheritance)
java competitive programming memo
[Memo] Java Linked List