Java Programming Tutorial Index

Decision Control Structures

If statements in Java is used to control the program flow based on some condition, it's used to execute some statement code block if the expression evaluated to true; otherwise, it will get skipped. This statement is the simplest way to modify the control flow of the program.



The basic format of if statement is:

Syntax:

if(test_expression)
{
    statement 1;
    statement 2;
    ...
}

'Statement n' can be a statement or a set of statements, and if the test expression evaluated to true, the statement block will get executed, or it will get skipped.

Figure - Flowchart of if Statement:

java-if

Example of a Java Program to Demonstrate If statements

Example:

public class Sample{

   public static void main(String args[]){
      int a=20, b=30;

      if(b>a)
         System.out.println("b is greater");
      }
}

Program Output:

java-if-statements



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram