Decision Making Statements

Decision Making Statements


Posted in : Core Java Posted on : September 28, 2010 at 12:39 PM Comments : [ 0 ]

Decision Making Statements

Decision Making Statements

Following are the types of decision making statements :

1. if statements

2. switch statements

Using  these statements, you control the flow of your program execution.

if statement

The if statement is conditional branch statement. Using it , you can route program execution through two different path. The syntax is given below :

if (condition) statement1;
else statement 2;

Example :

public static void main(String args[]){
int a = 30;

if( a < 32 ){
System.out.print("It is inside if control statement");
}}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac If.java

C:\Program Files\Java\jdk1.6.0_18\bin>java If
It is inside if control statement
It is inside if control statement

if...else Statement :

The if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Example :

public class IfElse {
   public static void main(String args[]){
      int x = 5;

      if( x < 3 ){
         System.out.print("Inside if statement");
      }else{
         System.out.print("Inside else statement");
      }
   }
}

Example :

C:\Program Files\Java\jdk1.6.0_18\bin>javac IfElse.java

C:\Program Files\Java\jdk1.6.0_18\bin>java IfElse
Inside else statement

Nested ifs

You can create nested if inside if. On thing keep in mind that an else statement always refers to the nearest if statement that is within the same block.

Example :

public class NestedIf {
public static void main(String args[]){
int a = 3;
int b = 30;

if( a == 3 ){
if( b == 30 ){
System.out.print("a = 3 and b = 30");
}
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac NestedIf.java

C:\Program Files\Java\jdk1.6.0_18\bin>java NestedIf
a = 3 and b = 30

if-else-if Statement

This statement structure is slightly different. It contains the sequence of "else if" after first "if" but it checks the condition same as normal if ,also it got control when previous if or else if is not executed.

Example :

public class IfElseIf {
   public static void main(String args[]){
      int x = 30;

      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("else statement");
      }
   }
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac NestedIf.java

C:\Program Files\Java\jdk1.6.0_18\bin>java NestedIf
Value of X is 30

Switch Statement

Switch statement provide an easy way to dispatch execution to different parts of your code based on the value of an expression.

The syntax is given below :

switch(expression){
    case value :
       //Statements
       break; //optional
    case value :
       //Statements
       break; //optional
    //You can have any number of case statements.
    default : //Optional
       //Statements
}

The 'expression" must be of type byte, short, int, or char. A switch statement can 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.

Example :

package corejava;

public class Switch {
private static final char c='A';

public static void main(String args[]){


switch(c)
{
case 'A' :
System.out.println("Excellent!"); 
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}

System.out.println("Your grade is " + c);
}

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javacSwitch.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Switch
Excellent!
Your grade is A

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics