A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/if_statement_in_java.htm below:

Java - if Statement

Java - if Statement Java if Statement

A Java if statement consists of a Boolean expression followed by one or more statements.

Syntax of if Statement

Following is the syntax of an if statement −

if(Boolean_expression) {
   // Statements will execute if the Boolean expression is true
}
Working of if Statement

If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed.

Flow Diagram of if Statement Java if Statement Examples Example 1

In this example, we're showing the use of a if statement to check if a value of variable, x is less than 20. As x is less than 20, the statement within if block will be printed.

public class Test {

   public static void main(String args[]) {
      int x = 10;

      if( x < 20 ) {
         System.out.print("This is if statement");
      }
   }
}
Output
This is if statement.
Example 2

In this example, we're showing the use of a if statement to check if a boolean value is true or false. As x is less than 20, result will be true and the statement within if block will be printed.

public class Test {

   public static void main(String args[]) {
      int x = 10;
      boolean result = x < 20;

      if( result ) {
         System.out.print("This is if statement");
      }
   }
}
Output
This is if statement.
Example 3

In this example, we're showing the use of a if statement to check if a boolean value is false. As x is not greater than 20, result will be true and the statement within if block will be printed.

public class Test {

   public static void main(String args[]) {
      int x = 10;
      boolean result = x > 20;

      if( !result ) {
         System.out.print("This is if statement");
      }
   }
}
Output
This is if statement.

java_decision_making.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4