noscript

Java Control statement is used to execute the statement based on the condition that you want to execute when the program is in the execution state. So the compiler checks the condition and if the condition is true then it executes the statement but if the condition is false then it will execute the rest of the code.

Types of Control Statement

  1. if statement
  2. if-else statement
  3. nested if statement
  4. if-else-if statement (if-else ladder statement)

 

if statement :

if is a control statement in the Java programming language which is used to check the condition and if a condition is true then it will execute the statement which is written in the if statement.

Flow Diagram:

Syntax :

if(condition){

//statement

}

Example:

public class IfStatementDemo {

        public static void main(String[] args) {

          int a=10;

          if(a<=20) {

System.out.println(a + ” is less than 20″);

}

}

}

if-else statement:

if-else is a control statement in the Java programming language which is used to check the condition and if a condition is true then it will execute the statement which is written in the if statement but if the condition is false then the control should come to the else part and execute the else block statement.

Flow Diagram:

Syntax :

if(condition){

//statement

}

else{ //Statement }

Example:

package com.javademo;

  public class IfStatementDemo {

          public static void main(String[] args) {

            int a=10;

            if(a<=20) {

System.out.println(a + ” is less than 20″);

}

            else {

System.out.println(a + ” is greater than 20″);

}

}

}

Nested if-else statement

Nested if-else is a control statement in the Java programming language which is used to check multiple conditions depending on the outer condition i.e. if the first i.e. outer condition is true then control comes inside the inner condition and checks the condition if it is true then execute the statement but if it is false then else part will be executed by the compiler.

Flow Diagram:

 

Syntax :

if(condition){

//statement

if(condition){

//statement

}

else{

//statement

}

}

else{ //Statement }

Example:

package com.javademo;

  public class IfStatementDemo {

         public static void main(String[] args) {

           int a=10;

           if(a<=20) {

             if(a>=5) {

System.out.println(a + ” is less than 20 and greater                          than 5″);

}

}

           else {

System.out.println(a + ” is greater than 20″);

}

}

}

if-else-if statement (if-else ladder)

if-else-if statement is a conditional statement in Java programming language. It is used to check the multiple conditions depending on the conditional statement. In this type, the compiler checks the first condition if it is true then executes the respected statement but if it is false then it will move to the next condition and check if it is true then execute the statement but if it is false then come to next condition and so on.

Flow Diagram:

Syntax:

if(condition1){

//statement

}

else if(condition2){

//statement

}

else if(condition3){

//statement

}

else if(conditionN){

//statement

}

else{

//statement

}
Example:

package com.javademo;

public class IfStatementDemo {

        public static void main(String[] args) {

          int a=10;

          if(a<=20) {

System.out.println(a + ” is less than 20″);

}

          else if(a>=20) {

System.out.println(a + ” is greater than 20″);

}

          else {

System.out.println(a + ” is equal to 20″);

}

}

}

For more information & classes Call: 7030000325
Registration Link: Click Here!

Author: Ambarish Durani

JAVA Trainer

IT Education Centre Placement & Training Institute

© Copyright 2023 | IT Education Centre.