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į!");
}
Console output

Pasiimk skėtį!

  1. The first line creates a Boolean variable, lyja.
  2. It stores the value true, meaning “yes, it is raining”.
  3. if checks the variable's lyja value.
  4. Since the condition is true, Java enters the block inside the braces and displays the text Pasiimk skėtį!.
What if the value were false?

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:

Java comparison operators
The operatorMeaningExample
<Less thana < b
<=Less than or equal toa <= b
>Greater thana > b
>=Greater than or equal toa >= b
==Equal toa == b
!=Not equal toa != b

Java conditional statements

  • if specifies code to run when the condition is true.
  • else specifies an alternative when that condition is false.
  • else if checks a new condition if the previous one was false.
  • switch lets you select one of many code blocks.

if statement syntax

if (sąlyga) {
    // This code runs if the condition is true
}
  1. After the keyword if , write the condition to check in parentheses.
  2. The condition must evaluate to true or false.
  3. If the result is true, the statements inside the braces run: { }.
  4. If the result is false, Java skips that block and continues after it.
This template produces no console output.

The words sąlyga and the comment mark where the programmer must write the actual condition and executable code.

Java keywords are case-sensitive.

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");
}
Console output

12 yra daugiau už 8

  1. The operator > asks: “Is the number on the left greater?”
  2. Java checks the comparison 12 > 8 and obtains the answer true.
  3. 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ų");
}
Console output

Jonas surinko daugiau taškų

  1. The variable jonasTaskai stores the number 20, while onaTaskai – 18.
  2. In the condition, Java uses the stored values in place of the variable names.
  3. It checks 20 > 18. The answer is true.
  4. 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");
}
Console output

Rūta ir Mantas surinko po lygiai

  1. Rūta's and Mantas's variables store the same value: 20.
  2. Two equals signs == ask whether both values are the same.
  3. The comparison 20 == 20 produces the answer true.
  4. Java executes the if block and displays the text about an equal result.
Do not confuse == 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");
}
Console output

Ona gali įeiti

  1. The variable onosAmzius stores Ona's age, 20, while onaTuriBilieta stores the value true.
  2. The first condition onosAmzius >= 18 is true because 20 is greater than or equal to 18.
  3. The second condition is also true because Ona has a ticket.
  4. The operator && means “and”: both sides must be true.
  5. Since both conditions are satisfied, the console displays Ona gali įeiti.
If at least one condition were false

For example, if Ona had no ticket, the entire comparison using && would be false and nothing would appear in the console.