header

For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

if else in java

 If else in Java



If-else condition in Java is similar to if-else in C/c++ programming languages. It is used to check whether a particular condition is satisfied or not. if the condition is satisfied it print the statements otherwise it moves to the else block.
We can use single if also to check a condition.

1) if condition :
Syntax :

if(condition){
group of statements
}


Example program showing if condition :

package Hello;
public class TestIf {

 private static int i = 5;

 public static void main(String[] args) {
 
  if(i>3){
System.out.println("Number is greater than 3");
  }
 }
}

Output : 
Number is greater than 3


2) if else condition :
Syntax :

if(condition){
group of statements
}
else{
group of statements
}

Example program showing if condition :

package Hello;

public class TestIfElse {

 private static int i = 2;

 public static void main(String[] args) {
 
  if(i>3){
System.out.println("Number is greater than 3");
  }
  else{
System.out.println("Number is less than 3");
  }
 
 }

}
Output : 
Number is less than 3

3) if - else if : 
Syntax :

if(condition){
group of statements
}
else if{
group of statements
}
else{
group of statements
}

Here is the simple program to check the value of an integer, whether it is greater than, less than, or equal to 5.

package Hello;

public class TestIfElseIf {

 private static int i=3;

 public static void main(String[] args) {
 
  if(i<5){
  System.out.println("i is less than 5");
  
  }
  else if(i==5){
  System.out.println("i is equal to 5");
  }
  else{
  System.out.println("i is greater than 5");
  }
   }
}

Output : 
i is less than 5


4) Nested if in Java

if block in some other if block is known as nested if block.

Here are some programs showing nested if conditions.

package Hello;

public class TestNestedIf{

 private static int i=3;

 public static void main(String[] args) {
 
  if(i<5){
System.out.println("i is less than 5");
  if(i>2){

System.out.println("i is greater than 2 less than 5");
   }
 
  }
   }
}

Output : 
i is greater than 2 less than 5

Here is another program : 

package Hello;

public class TestNestedIf2 {

 private static int i=2;

 public static void main(String[] args) {
 
  if(i<5){
  System.out.println("i is less than 5");
   if(i>2){
  System.out.println("i is greater than 2 less than 5");
   }
   else{
  System.out.println("i is less than or equal to 2");
   }
 
  }
 
 }

}

Output : 
i is less than 5
i is less than or equal to 2

program showing if-else in else block : 

package Hello;

public class TestNestedIf2 {

 private static int i=9;

 public static void main(String[] args) {
 
  if(i<5){
   System.out.println("i is less than 5");
  }
   else{
    if(i==5){
     System.out.println("i is equal to 5");
    }
    else{
    System.out.println("i is greater than 5");
    }
   }
 
  }
 
 }


Output : 
i is greater than 5

Now a simple program showing if and else-if block in else block

package Hello;

public class TestNestedIf2 {

 private static int i=7;

 public static void main(String[] args) {
 
  if(i<5){
System.out.println("i is less than 5");
  }
   else{
    if(i==5){
System.out.println("i is equal to 5");
    }
    else if(i>5 && i<7){
System.out.println("i is greater than 5 and less than 7");
    }
    else{
System.out.println("i is greater than or equal to 7");
    }
   
   }
   
  }
 
 }

Output
i is greater than or equal to 7

Multiple if conditions : 

Syntax : 

if(condition){
statements
}
if(condition){
statements
}
if(condition){
statements
}
.................

Sample program showing multiple if conditions

package Hello;

public class TestMultipleIf {

 private static int i=8;

 public static void main(String[] args) {
 
  if(i>2){
System.out.println("i is greater than 2");
  }
  if(i>5){
System.out.println("i is greater than 5");
  }
  if(i>7){
System.out.println("i is greater than 7");
  }
 
  }
 
 }

Output : 
i is greater than 2
i is greater than 5
i is greater than 7


No comments:

Post a Comment



Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER