Line At A Time
Lesson 10: OR Operator
OR Operator Combine conditions with || (OR).
1public static void main(String[] args) {
2 boolean isTrue = true;
3 boolean isFalse = false;
4 if (isTrue || isFalse) {
5 System.out.println("at least one is true");
6 }
7}
Terminal
$ javac Main.java
$ java Main
at least one is true
Every line, explained
public static void main(String[] args) {
Every Java program starts at the main method. When you run the program, Java looks for this exact line, public static void main(String[] args), and runs everything between its { and } from top to bottom.
boolean isTrue = true;
boolean declares a variable that can store only one of two values: true or false. Watch the spelling: in Java the type is boolean; writing bool (like some other languages use) will not compile.
boolean isFalse = false;
boolean declares a variable that can store only one of two values: true or false. Watch the spelling: in Java the type is boolean; writing bool (like some other languages use) will not compile.
if (isTrue || isFalse) {
if checks the condition inside its parentheses. If the condition is true, Java runs the code between the { and }. If it is false, Java skips that code completely. || means OR: the whole condition is true if at least one side is true. true || false is true, so the block runs.
System.out.println("at least one is true");
System.out.println(...) prints whatever is inside the parentheses to the terminal, then moves to a new line. Read the name carefully: print-l-n is short for "print line".
}
This } closes the if block.
}
This closing curly brace } ends the main method. In Java every opening brace { must have a matching closing brace }.