JAVA CONDITIONS
Java if statement
Conditions and the if statement let you control program flow: deciding which code runs and which is skipped.
A condition must return true or false
Imagine an everyday decision: if it rains, take an umbrella. In Java, this decision is expressed as a condition that produces a Boolean boolean value.
boolean lyja = true;
if (lyja) {
System.out.println("Pasiimk skėtį!");
}Pasiimk skėtį!
- The first line creates a Boolean variable,
lyja. - It stores the value
true, meaning “yes, it is raining”. ifchecks the variable'slyjavalue.- Since the condition is true, Java enters the block inside the braces and displays the text Pasiimk skėtį!.
Java would skip the entire if block, so this program would display nothing in the console.
Comparison operators in conditions
Conditions are often created by comparing two values:
| The operator | Meaning | Example |
|---|---|---|
< | Less than | a < b |
<= | Less than or equal to | a <= b |
> | Greater than | a > b |
>= | Greater than or equal to | a >= b |
== | Equal to | a == b |
!= | Not equal to | a != b |
Java conditional statements
ifspecifies code to run when the condition is true.elsespecifies an alternative when that condition is false.else ifchecks a new condition if the previous one was false.switchlets you select one of many code blocks.
if statement syntax
if (sąlyga) {
// This code runs if the condition is true
}- After the keyword
if, write the condition to check in parentheses. - The condition must evaluate to
trueorfalse. - If the result is
true, the statements inside the braces run:{ }. - If the result is
false, Java skips that block and continues after it.
The words sąlyga and the comment mark where the programmer must write the actual condition and executable code.
Write if in lowercase. If or IF will cause a compilation error.
Comparing values and variables
You can directly check whether one value is greater than another:
if (12 > 8) {
System.out.println("12 yra daugiau už 8");
}12 yra daugiau už 8
- The operator
>asks: “Is the number on the left greater?” - Java checks the comparison
12 > 8and obtains the answertrue. - The line inside the block therefore runs and displays the text 12 yra daugiau už 8.
In practice, variables are most often compared:
int jonasTaskai = 20;
int onaTaskai = 18;
if (jonasTaskai > onaTaskai) {
System.out.println("Jonas surinko daugiau taškų");
}Jonas surinko daugiau taškų
- The variable
jonasTaskaistores the number20, whileonaTaskai–18. - In the condition, Java uses the stored values in place of the variable names.
- It checks
20 > 18. The answer istrue. - The console therefore displays an explanation that Jonas scored more points.
Equality is checked with the operator ==:
int rutaTaskai = 20;
int mantasTaskai = 20;
if (rutaTaskai == mantasTaskai) {
System.out.println("Rūta ir Mantas surinko po lygiai");
}Rūta ir Mantas surinko po lygiai
- Rūta's and Mantas's variables store the same value:
20. - Two equals signs
==ask whether both values are the same. - The comparison
20 == 20produces the answertrue. - Java executes the
ifblock and displays the text about an equal result.
== and =.== compares values, while = assigns a value to a variable.
Combining conditions
The logical operator && allows the block to run only when both conditions are true:
int onosAmzius = 20;
boolean onaTuriBilieta = true;
if (onosAmzius >= 18 && onaTuriBilieta) {
System.out.println("Ona gali įeiti");
}Ona gali įeiti
- The variable
onosAmziusstores Ona's age,20, whileonaTuriBilietastores the valuetrue. - The first condition
onosAmzius >= 18is true because20is greater than or equal to18. - The second condition is also true because Ona has a ticket.
- The operator
&&means “and”: both sides must betrue. - Since both conditions are satisfied, the console displays Ona gali įeiti.
For example, if Ona had no ticket, the entire comparison using && would be false and nothing would appear in the console.