30 days Java Diary — Day2

Elsa.Chen
2 min readJul 9, 2021

Today we will learn the Control flow in Java.

Comparison Operators

Things to note:

a. Priority ! > && > ||

b. && Short circuits. If the first condition is not satisfied. The rest of the condition will not be evaluated.

Control Flow — Conditional Statement

things to note:

a. {} can be eliminated if the statement is a one-liner and not a declaration statement.

b. The Tneray Operator to simplify the code:

int income = 120_000;
String className = income > 100_000 ? "First" : "Economy"

c. Switch Statement:

Loop In Java

  1. For Loop

things to note: Break statement to break out entire loop operation while Continue Statement to break out current iteration and move on to the next element of the iterable.

2. While Loop

//if you have a condition to break the loop in side the statement, use pattern
while(true){
....
if(condition)
break;
}

3. do-while loop

4. for-each loop

A common CLI Validation Pattern:

declare a variable
while(true){
get user input
if user input is valid:
do something..
break
output "input have to be valid" }

--

--