Conditional statements in programming

Conditional Statements uses one or more conditions to execute a statement or set of statements.If the given condition or conditions are true then statements or set of statements will be executed otherwise not.Suppose we wanted to find number of male members in a family we will apply condition if gender is male then display it otherwise not.The following conditional statements can be used in java.All conditions will be given as relational expressions.

1) If statement
If statement is used to execute or skip a block of statements.If condition is true complete block will be executed  otherwise complete block will be skipped.It is most simplest conditional statement in java.
Syntax:
if(condition/set of conditions)
{
//Statements here
}

2) If else statement
If else statement is same as if statement.The main difference is that if the condition becomes wrong in if statement nothing will be displayed.But in if else statement there are two blocks of statements.If first block is skipped then  second block will be executed and if first block is executed then second block will be skipped.But both block of  statements can never be executed and both block of codes can never be skipped.
The syntax of an if-else statement in java  language is:

if(condition 1)
{
   /* Executes when the condition 1 is true */
}
else
{
   /* Executes when the above condition is not true */
}


3)The if-else-if Statement
An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.When using if , else if  statements there are few points to keep in mind:
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else-if's and they must come before the else.
Once an else if succeeds, none of the remaining bolcks will be tested or executed.
Syntax:

The syntax of an if-else-if statement in java  language is:

if(condition 1)
{
   /* Executes when the condition 1 is true */
}
else if(condition 2)
{
   /* Executes when the condition 2 is true */
}
else if(condition 3)
{
   /* Executes when the condition 3 is true */
}
else 
{
   /* executes when the none of the above condition is true */
}

4) Nested if Statement
Java programming allows programmers to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

The syntax for a nested if statement is as follows:

if(Condition 1)
{
   /* Executes when the Condition 1 is true */
   if(Condition 2)
   {
      /* Executes when the Condition 2 is true */
   }
}

You can nest else-if blocks and else blocks in the similar way as you have nested if statement.

5)Switch statement
Switch statement is vest alternative of if else.A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

The syntax for a switch statement in java language is as follows:

switch(expression){
    case constant-expression  :
       statement(s);
       break; /* optional */
    case constant-expression  :
       statement(s);
       break; /* optional */
  
    /* you can have any number of case statements */
    default :

       statement(s);
}

The following rules apply to a switch statement:
The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. 


6) Nested Switch
It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
The syntax for a nested switch statement is as follows:

switch(ch1) {
   case 'A': 
      switch(ch2) {
         case 'A':
            //Statements
            break;
         case 'B': /* case code */
      }
      break;
   case 'B': /* case code */
}

Reactions

Post a Comment

0 Comments