Java Conditional Statements

Java Conditional Statements   
Conditional statements are used to insert verification points and for Error handling.

Two types of Conditional statements in Java
1) if statement

2) switch statement
------------------------------
Types of Conditions
1) Single condition
Ex:

if (a > b) {
----
----

2) Compound condition
Ex:

if ((a > b) && or || (a > c)) {
--------
----------

3) Nested Condition
if (a > b){}
 if (a < c) {}
  if (a < d) {
----
----
}
------------------------------
Usage of Conditional Statements
1) Executing a block of statements when condition is true.
Syntax:
if (Condition) {
Statements
---------
---------
}

Example:public static void main (String [] args){
        int a, b;
        a =10;
        b =5;
       
        if (a > b) {
            System.out.println("A is a Big Nuber");
        }
------------------------------------
2) Executing a block of statements when compound condition is true
Syntax:
if ((condition) && (condition2)) {
Statements
----------
-----------
}

Example:int a, b, c = 2;
        a =10;
        b =5;
       
        if ((a > b) && (a > c)) {
            System.out.println("A is a Big Nuber");
        }
-------------------------------------------
3) Executing a block of statements when condition is true. otherwise executing another block of statements.
Syntax:
if (Condition) {
Statements
----------
--------
}
else {
statements
---------
--------
}

Example:
public static void main (String [] args){
        int a, b;
        a =10;
        b =50;
       
        if (a > b) {
            System.out.println("A is a Big Number");
        }
        else {
            System.out.println("B is a Big Number");
        }
-----------------------------------------
4) Decide among several Alternates (else if structure)
Syntax:
if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else if (condition) {
Statements
------
---------
}
else
{
statements
----------
--------
}

Example:
public static void main (String [] args){
        int a = -100;
               
        if ((a >= 1) && (a < 100)) {
            System.out.println("A is a Small Number");
        }
        else if ((a > 100) && (a <= 1000)) {
            System.out.println("A is a Medium Number");
            }
        else if (a > 1000) {
            System.out.println("A is a Big Number");
            }
        else
        {
            System.out.println("A is either Zero or negative value");
        }
--------------------------------------------       
5) Executing a block of statements when more than one condition is true (Nested if).
Syntax:
if (condition) {}
 if (condition) {}
  if (condition) {
Statements
--------
---------
}
else
{
Statements
---------
---------
}

Example:
public static void main (String [] args){
        int a =10, b =7, c = 5, d = 13;
               
        if (a > b){}
          if (a > c) {}
           if (a > d){
               System.out.println("A is a Big Number");
           }
           else {
               System.out.println("A is Not a Big Number");
           }
---------------------------------------
6) Decide among several alternates (using Switch case structure).
Syntax:
switch (expression) {

case value:
Statements
-------
break;
case value:
Statements
-------
break;   
case value:
Statements
-------
break;
default
Statements
-------
------
}

Example:char grade = 'X';
       
        switch (grade){
       
        case 'A':
            System.out.println ("Excellent");
            break;
        case 'B':
            System.out.println ("Well Done");
            break;
           
        case 'C':
            System.out.println ("Better");
            break;
        default:
            System.out.println("Invalid Grade");
                }
---------------------------------------------------------

0 comments:

Post a Comment