中級 Java
Jim White
Java Developer
条件が true のとき ✅ ➡ 何かを実行
条件が false のとき ❌ ➡ 何もしない
score が 90 以上なら ➡ "Great job!" を出力
90 未満なら ➡ 出力しない
if (condition) {
// Code to run
}
条件が true のとき、中かっこ内のコードが実行されます
==, !=, >, <, >=, <= などの比較演算子を使用// score が >= 90 のとき "Great job!" を出力
if (score >= 90) {
System.out.println("Great job!");
}
int score = 94;
if (score >= 90) {
System.out.println("Excellent!");
}
if (score >= 70) {
System.out.println("Good job!");
}
if (score < 70) {
System.out.println("Try again!");
}
Excellent!
Good job!
条件は true /false に評価され、別々に扱えます
if (score != 0){
System.out.println("Well, you got some points!");
}
if (message.equals("F")){
System.out.println("Try again!");
}
if (testResult == 100) {
System.out.println("Wow, you got it all!");
}
中級 Java