Booleans and selection: if/else and logic
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Booleans and selection
- A boolean is a value that is either
trueorfalse. - Programs make choices by testing a boolean and running different code.
- In this lesson you learn
if/else if/elseand how to build conditions.
日本語
ブール値と条件分岐
- ブール値とは、
trueまたはfalseのどちらかである値のことです。 - プログラムはブール値をテストして、異なるコードを実行することで選択を行います。
- このレッスンでは
if/else if/elseを学び、条件式を作成する方法を習得します。
English
Relational operators
- These compare two values and give a
boolean:<less than,>greater than<=less or equal,>=greater or equal==equal,!=not equal
- Note:
=puts a value into a variable, but==tests if two values are equal.
日本語
関係演算子
- これらは2つの値を比較し、
booleanを返します:<未満、>大なり<=以下、>=以上==等しい、!=等しくない
- 注意:
=は値を変数に代入しますが、==は2つの値が等しいかをテストします。
public class Main {
public static void main(String[] args) {
int score = 75;
System.out.println(score > 50); // true
System.out.println(score == 100); // false
System.out.println(score != 75); // false
}
}
English
if / else if / else
ifruns a block only when its condition istrue.else ifchecks another condition when the ones before werefalse.elseruns when none of the conditions were true.
日本語
if / else if / else
ifは条件がtrueの場合のみブロックを実行します。else ifは前の条件がfalseでなかった場合に別の条件をチェックします。elseはどの条件も真でなかった場合に実行されます。
public class Main {
public static void main(String[] args) {
int n = -4;
if (n > 0) {
System.out.println("positive");
} else if (n < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}
}
}
English
Logical operators
&&means and: true only when both sides are true.||means or: true when at least one side is true.!means not: it flipstruetofalseandfalsetotrue.
日本語
論理演算子
&&は and を意味します:両方の側が真の場合にのみ真です。||は or を意味します:少なくとも片方が真の場合に真です。!は not を意味します:trueをfalseに、falseをtrueに反転させます。
public class Main {
public static void main(String[] args) {
int age = 16;
boolean teen = age >= 13 && age <= 19;
System.out.println(teen); // true
System.out.println(!teen); // false
System.out.println(age < 13 || age > 19); // false
}
}
English
Short-circuit
- Java reads
&&and||from left to right and stops early when it can. - With
&&, if the left side isfalse, the answer is alreadyfalse, so the right side is skipped. - With
||, if the left side istrue, the answer is alreadytrue, so the right side is skipped. - This lets you check something is safe before you use it.
日本語
ショート・サーキット
- Java は
&&と||を左から右へ読み込み、判断できる場合は早期に停止します。 &&の場合、左側の側がfalseなら答えは既にfalseなので、右側の側はスキップされます。||の場合、左側の側がtrueなら答えは既にtrueなので、右側の側はスキップされます。- これにより、使用前に安全かどうかをチェックできます。
public class Main {
public static void main(String[] args) {
int x = 0;
// Left side is false, so x / x is never run (no error).
boolean ok = (x != 0) && (10 / x > 1);
System.out.println(ok); // false
}
}
English
The == vs .equals() String trap
- For
intandboolean,==works fine. - For
String,==checks if they are the same object in memory, not the same letters. - Two Strings with the same letters can still be different objects, so
==may befalse. - Always compare Strings with
.equals(...).
日本語
== vs .equals() Stringの罠
intとbooleanについては、==が適切に機能します。Stringについては、==は同じ文字列かどうかではなく、メモリ上の同一オブジェクトかどうかをチェックします。- 同じ文字列を持つ2つのStringは依然として異なるオブジェクトであるため、
==はfalseになることがあります。 - String を比較する際は、常に
.equals(...)を使用してください。
public class Main {
public static void main(String[] args) {
String a = "cat";
String b = new String("cat");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same letters)
}
}
English
Common mistakes
- A condition must be a
boolean;if (x = 5)does not compile. - Each
switchcase needs abreak;.
日本語
よくあるミス
- 条件式は
booleanでなければならず、if (x = 5)ではコンパイルできません。 - 各
switchのケースにはbreak;が必要です。
English
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
日本語
あなたも試してみよう
- 各タスクはクラス骨組みを事前入力しています。main 内にコードを書くか、表示されたメソッドを完成させてください。
- 実行 を押してコンパイル・実行し、その後 回答を確認 を押してください。
- コードはサーバー上でコンパイル・実行されるため、初回実行でも高速です。
Explore · 探索
Boolean logic (and/or/not)
&&, ||, ! combine true/false values — see the truth table.
The variable n is -4. Use if / else if / else to print exactly one word: positive if n > 0, negative if n < 0, or zero otherwise.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete equal(String a, String b) so it returns true when the two Strings have the same letters. Use .equals, not ==.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete inRange(int x) so it returns true when x is between 10 and 20, including both ends (10 and 20 count). Use &&.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。