[フレーム]

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java this Keyword Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java super Keyword Java Inner Classes Java Abstraction Java Interface Java Enums Java User Input Java Date

Java Errors

Java Errors Java Debugging Java Exceptions

Java File Handling

Java Files Java Create/Write Files Java Read Files Java Delete Files

Java Data Structures

Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator

Java Advanced

Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting

Java How To's

Add Two Numbers Count Words Reverse a String Sum of Array Elements Convert String to Array Sort an Array Find Array Average Find Smallest Element ArrayList Loop HashMap Loop Loop Through an Enum Area of Rectangle Even or Odd Number Positive or Negative Square Root Random Number

Java Reference

Java Reference Java Keywords Java String Methods Java Math Methods Java Output Methods Java Arrays Methods Java ArrayList Methods Java LinkedList Methods Java HashMap Methods Java Scanner Methods Java Iterator Methods Java Errors & Exceptions

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Server Java Syllabus Java Study Plan Java Certificate


Java If ... Else


Java Conditions and If Statements

Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.

Think of it like real life: If it rains, take an umbrella. Otherwise, do nothing.

Every if statement needs a condition that results in true or false.

This means if statements work hand-in-hand with boolean values:

Example

boolean isRaining = true;
if (isRaining) {
 System.out.println("Bring an umbrella!");
}

Try it Yourself »

Most often, conditions are created using comparison operators, like the ones below:

  • 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

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax

if (condition) {
 // block of code to be executed if the condition is true
}

The condition inside the if statement must result in a boolean value - it can be either a boolean expression (like x > y) or a boolean variable (like isLightOn).

Also note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the condition is true, print some text:

Example

if (20 > 18) {
 System.out.println("20 is greater than 18");
}

Try it Yourself »

We can also test variables:

Example

int x = 20;
int y = 18;
if (x > y) {
 System.out.println("x is greater than y");
}

Try it Yourself »

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the > operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that "x is greater than y".

Comparison is also often used to check if two values are equal, using the == operator:

Example

int x = 20;
int y = 20;
if (x == y) {
 System.out.println("x is equal to y");
}

Try it Yourself »

Here the condition x == y is true, because both x and y are 20, so the message "x is equal to y" is printed.


Using Boolean Variables

You can also test boolean variables directly in an if statement:

Example

boolean isLightOn = true;
if (isLightOn) {
 System.out.println("The light is on.");
}

Try it Yourself »

Note: Writing if (isLightOn) is the same as writing if (isLightOn == true), but shorter and easier to read.

Here is the same example with the value false to see that the program continues even when the code block does not run:

Example

boolean isLightOn = false;
if (isLightOn) {
 System.out.println("The light is on."); // This will not be printed
}
System.out.println("This line always runs.");

Try it Yourself »

In the next chapters, you will learn how to run code when the condition is false (using else), or when there are several conditions to check (using else if).




Track your progress - it's free!
×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

AltStyle によって変換されたページ (->オリジナル) /