Studying Java ②

Summary of comparison, logical operators and conditional branching, iterative processing

Comparison operator

The comparison operator is a symbol that compares values and returns true or false. The following comparison operators are available.

operator Example Description
> x > y true when x is greater than y
>= x >= y True if x is greater than or equal to y
< x < y true when x is less than y
<= x <= y True if x is less than or equal to y
== x == y true when x and y are equal
!= x != y true when x and y are not equal
instanceof x instanceof y true if x is the same class as y or a subclass of y

Precautions when using == and! =

Java data types are roughly divided into basic types (primitive types) and reference types. There is nothing wrong with using ==! = For base types, but be careful when using it for reference types. When using ==! = For a reference type, you need to be aware that you are not comparing the values held by the object, but comparing whether the referrers referenced by the object are the same. There is. If you want to compare the values held by the objects, use the equals method. Source) Java Road: Operator (2. Comparison Operator)

I haven't done Java enough to be aware of how to use it properly, but I think it's about to get caught. Reference) Difference between == operator and equals method

Logical operator

&& x && y ・ ・ ・ Returns true when both x and y are true.

//x is 1
x > 5 && x < 15   //false
//x is 10
x > 5 && x < 15   //true
//x is 20
x > 5 && x < 15   //false

||

x ||y ・ ・ ・ Returns true when either x or y is true.

//x is 1
x < 5 || x > 15   //true
//x is 10
x < 5 || x > 15   //false
//x is 20
x < 5 || x > 15   //true

! ! x ・ ・ ・ Returns true if x is false.

//x is 10
x >= 100      //false
!(x >= 100)   //true

if statement

if (Conditional expression) {
Process 1;
} else {
Process 2;
}

When the conditional expression is true, process 1 is executed. When the conditional expression is false, process 2 is executed. else can be omitted.

In addition, conditional branching is possible using else if as shown below.

if (Conditional expression A) {
Process A;
} else if (Conditional expression B) {
Process B;
} else if (Conditional expression C) {
Process C;
} else {
Process D;
}

If conditional expression A is false, conditional expression B is evaluated, and if it is true, process B is executed. If conditional expression B is also false, conditional expression C is evaluated, and if true, processing C is executed. If all are false, process D below else is executed. You can write as many else ifs as you like. else can be omitted.

switch statement

The if statement uses a Boolean value (true, false) as the criterion, while the switch statement uses an integer value as the criterion.

switch (Conditional expression) {
case value 1:
Process 1;
    break;
case value 2:
Process 2;
    break;
  default:
Process 3;
    break;
}

First, the conditional expression is determined and the processing of the matching case value is executed. break is the process of breaking out of the switch statement. For example, if the result of the conditional expression matches the value 2, the process 2 is executed, and the switch statement is exited by the subsequent break. If it does not match any case value, the default process 3 is performed and the break exits.

default and break can be omitted. However, if you do not write ** break, you will not get out of the process and will execute the process of the next case. ** **

//When the conditional expression matches the value 1
switch (Conditional expression) {
case value 1:
Process 1;      //Since there is no break, go to the next case without getting out
case value 2:
Process 2;      //Process 2 is also executed
    break;      //Exit here
}

It seems better to write break unless you want to intentionally execute multiple cases. (Is there such a situation?)

while statement

int i = 1;
while (i <= 3) {
  System.out.println("i is" + i + "is");
  i++;
}

/*result
i is 1
i is 2
i is 3
*/

Determine the inside of the conditional expression, and if true, execute the processing of {}. After that, the conditional expression is determined again and repeated as long as it remains true. If false, exit. If it is false from the beginning, the process is never executed.

As long as the conditional expression is true, the loop is repeated, so if you do not write the process so that it will be false someday, it will be an infinite loop **. In the above case, if i ++; is not written, an infinite loop will occur.

for statement

for (Variable initialization;Conditional expression;Variable update) {
processing;
}

In the variable initialization, the local variables (int i = 0; etc.) used to judge the iterative processing are initialized. In the conditional expression, write a condition that returns a Boolean type value (true, false). After processing, update the variable (i ++ etc.).

for (int i = 0; i < 5; i++) {
  System.out.println("i is" + i + "is");
}

/*result
i is 0
i is 1
i is 2
*/

break and continue

break It is possible to forcibly terminate the iterative process by using break. Combine with conditional branching such as if statement.

for (int i = 1; i <= 5; i++) {
  if(i > 3) {   //Forced termination when i reaches 4
    break;
  }
  System.out.println("i is" + i + "is");
}

/*result
i is 1
i is 2
i is 3
*/

continue Only the processing of that week is skipped. Combine with conditional branching such as if statement.

for (int i = 1; i <= 5; i++) {
  if (i % 2 == 0) {   //When i is an even number, the loop around that cycle is terminated and the next loop is started.
    continue;
  }
  System.out.println("i is" + i + "is");
}

/*result
i is 1
i is 3
i is 5
*/

Recommended Posts

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying Java 8 (Optional)
Studying Java 8 (Stream)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see method)
Studying Java 8 (see constructor)
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
Java protected
[Java] Annotation
Java array
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
Studying Java # 6 (How to write blocks)
java (array)
Java static
java beginner 4
Java (set)
java shellsort
[Java] compareTo
java reflexes
Java memorandum
☾ Java / Collection
Java array
[Java] Array
[Java] Polymorphism
Java review
java framework
[Java] Inheritance
FastScanner Java
java beginner 3
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface