Wednesday 15 August 2012

DECISION STATEMENT IN JAVA


Hi Friends  !!!! Today I am  going to tell about how  decision statements are used in java language..
Decision statements !!


First of all,as the word define decision,,these are those statements in which we decide weather which one statement is right or which one is wrong,……In  Java ,we generally use 2 types of decision making statements:1) if statements2) switch statements
If statement:
         This statement is generally used when the given condition inside if statement is right .Then that  block of code executes which is inside the if statement….
          Or
We can say , An if statement consists of a Boolean expression followed by one or more statements.
Syntax:

if(Boolean_expression)
{
   //Statements will execute if the Boolean expression is true
}
Example Of If Statement:
public class abc 
{
   public static void main(String args[])
{
      int x = 5;

      if( x < 20 )
{
         System.out.print("if statement");
      }
   }
}
Output
if statement


The if …else statement:
                   When we have both the situation,true and false then this statement will be used  Or in technical terms ,we can say that
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Syntax:
if(Boolean_expression){
   //Executes when the Boolean expression is true
}else{
   //Executes when the Boolean expression is false
}
Example of if...else Statement:
public class abc
{
   public static void main(String args[])
{
      int x = 5;

      if( x < 20 )
{
         System.out.print("if statement");
      }
else
{
         System.out.print(" else statement");
      }
   }
}
Output:
if statement

The if…elseif…else statement:
                An if statement can be followed by an optional else if...else statement, which is very usefull to test various conditions using single if...else if statement.
When using if , else if , else 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 he remaining else if's or else's will be tested.
Syntax:                             
if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
}else {
   //Executes when the none of the above condition is true.
}
Example of if....elseif..else Statement:
public class abc {
   public static void main(String args[]){
      int x = 5;

      if( x == 1 )
{
         System.out.print("Value of X is 1");
      }
else if( x == 2)
{
         System.out.print("Value of X is 2");
      }
else
{
         System.out.print("else statement");
      }
   }
}
output:


else statement


 Nested if…else statement…
      This means we can use use one or more then one if…else statement inside a if.. else statement
Syntax:
if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
   if(Boolean_expression 2){
      //Executes when the Boolean expression 2 is true
   }                          
}
Example Of nested if...else Statement:
public class abc
 {
   public static void main(String args[])
{
      int x = 1;
 int y = 2;

      if( x == 1 ){
         if( y == 2){
         System.out.print("X = 1 & Y = 2");
      }
   }
}
Output:
X=1 &  Y=2

 Switch statement:
           In switch statement,one variable is taken,n the value of variable is checken(prove to be equal) over a list of case value,if the value is matched then the block of code inside that statement will execute,.otherwise a default statement will execute….
Syntax:
switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}
Example Of Switch Statement:
public class abc
{
   public static void main(String args[])
{
      char gd = args[0].charAt(0);//this is the declaration of variable of char datatype

      switch(gd)
      {
      case 'A' :
         System.out.println("AWESOME"); 
         break;
      case 'B' :
         System.out.println("AWKWARD");
         break;

      default :
         System.out.println("Invalid gd");
      }
      System.out.println("Your grade is " + gd);
   }
}
output:
suppose we have to  press A
then,
AWESOME
your grade is A


Now !! You can download the copy of this article in pdf file by just click on download link..

If still You have any query regarding  to this  article then please post your comment i will 100% reply back  you…..Thanks……:)


0 comments:

Post a Comment

Hey thanax alot to comment i will revert you back soon...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites