Our social:

Wednesday, October 5, 2016

Condition Statements in Java-Tutorial

Every programming language offer some condition checking features and relevant action depending upon condition. Like if a condition satisfies then I have to do work A and if the condition does not satisfy, then I have to do work B. Or imagine that we have to continue doing some work C until some condition remains valid, and stops immediately when the condition is not valid anymore. In terms of java these are called control statement. It controls the execution of a program.
Java provide mainly three types of Control Statement, as figured below:


Here we will cover the Selection Statement.
You have to have the knowledge of basic data type before you master this concept. Please check the Data Type discussions first.

Java provides two types of Selection statement

1. If
2. Switch.

If Statement

Have a look at the below program for understanding If.

package com.java;

public class IfDemo {

 public static void main(String[] args) 
 {
  int modeSelection = 4;

  if (modeSelection == 0) {
   System.out.println("Return To main Menu Action");
  }

  else if (modeSelection == 1) {
   System.out.println("English language Selection");
  }

  else if (modeSelection == 2) {
   System.out.println("Hear again all the Option");
  }

  else if (modeSelection == 3) {
   System.out.println("Enter Phone Number Action");
  }

  else if (modeSelection == 4) {
   System.out.println("Choose Another Language");
  }

  else
   System.out.println("Exit");

 }

}
Try It
As you can see, there are two keywords in the program. If and else. ‘else’ used with if to tell the program what to do when a condition does not satisfy.
Interpret ‘else if’ in this way. If inside else you again want to check some condition then use if. Together it becomes ‘else if’.
The program above sets an integer variable modeSelection to 4. For modeSelection values 0 to 4 range we have some action defined. For any other number the system will exit.
This kind of situation is common in operator driven system, where before reaching a particular person/number you have to select lot of options in a phone call.

if (modeSelection == 0)
{
 System.out.println("Return To mail Menu Action");
}
else if (modeSelection == 1)
{
 System.out.println("English language Selection");
}
First it checks if the modeSelection is equal to 0. If it is then it will take the action to return to main menu. In our program since we have set modeSelection to 4 (or think that you have pressed 4 while selecting options), so the ‘if’ condition does not satisfies, so it will go to else part. There again we are checking whether the modeSelection is equal to 1. Since this is also not the case it will go to the next else part. In this way the program execution continues. Consider what happens when the program reaches at this point:
else if (modeSelection == 4)
{
 System.out.println("Choose Another Language");
}
Here the condition satisfies. So it enters the block (the println statement) and prints the output in the consol.
Check the last ‘else’ part. There we do not have to check for any specific number, we have already check for all the numbers from 0-4 range. If you enters any other numbers then simply the program exits.
The condition in if must be itself a boolean variable or some expression that gives boolean value (true/ false).
In the program above except for this section: else if (modeSelection == 4), every if condition gives false.
Run the program, as expected you will get the output as below:
Choose Another Language
You can imagine how the program will look like if you have only one condition check, it will look like: 
if(condition)
{
 statement1
} 
else
{
 statement2
}
As simple as that.
Probably you have guessed it by now that the condition part returns a boolean value, either ‘true’ or ‘false’. If it returns ‘true’ then the statement inside if executes , if it returns ‘false’ then it goes to next execution line, in the above snippet which is else part. Remember ‘if’ statement can exist alone, that is without else part. Sometime in your program you have to execute some statement if a condition satisfies, otherwise let the program continue as it is.
if(memoryOverUsed)
{
 statemt1
}
 
program continues
.....
There is another feature of if statement you have to know before we move to the switch statement. That is nested if.
Let’s modify a little bit the above program. Suppose that in order to continue the operation in the operator driven phone call if you have to first press the number less than 5. If you press anything greater than 5, then the program exits straight away. Have a look into the code below:
package com.java;

public class IfDemo 
{

 public static void main(String[] args) 
 {
  int modeSelection = 4;

  if (modeSelection < 5) 
  { // first if block starts here
   
   if (modeSelection == 0) { // second if block starts here
    System.out.println("Return To main Menu Action");
   } // second if block ends here

   else if (modeSelection == 1) {
    System.out.println("English language Selection");
   }

   else if (modeSelection == 2) {
    System.out.println("Hear again all the Option");
   }

   else if (modeSelection == 3) {
    System.out.println("Enter Phone Number Action");
   }

   else if (modeSelection == 4) {
    System.out.println("Choose Another Language");
   }
  } // first if block ends here

  else
   System.out.println("Exit");

 }

}
Try It
The program will only checks if the modeSelection is in the range of 0-4 if you have first pressed anything less than 5. Otherwise it makes no sense to check 0-4 range. Check the curly brackets carefully along with the comments.
So you have seen that how one if block can be inside an outer if block. For example in the above program the second if block is nested inside first outer if block.

Switch Statement

Switch is another way java provides condition checking. If we have to write the above program using switch instead of if-else then it will look like below:
package com.java;

public class SwitchDemo 
{

 public static void main(String[] args) 
 {
  int modeSelection = 4;

  switch (modeSelection) {
  case 0:
   System.out.println("Return To main Menu Action");
   break;

  case 1:
   System.out.println("English language Selection");
   break;

  case 2:
   System.out.println("Hear again all the Option");
   break;

  case 3:
   System.out.println("Enter Phone Number Action");
   break;

  case 4:
   System.out.println("Choose Another Language");
   break;

  default:
   System.out.println("Exit");
  }
 }
}
Try It
There are some rules you have to remember while using switch.
Check the basic structure of switch program:
switch (expression) 
{
 case value1:
 // statement1
 break;
 case value2:
 // statement2
 break;
 default:
 // default statement sequence
}
The expression must be int, short, byte and char. Java 7 also supports String in expression. (in the program above we have used int in expression)
The value in ‘case’ must be of same data type as you have used in the expression above. (all the values we have used in the four ‘case’ in the above program are integer, same as switch expression)
The ‘break’ works as a termination, so if a case match happens , the program come out of switch block without checking other cases(if other cases exist).
The ‘default’ is used for a situation where no cases is matched. In the above program if the modeSelection contains any value outside the range of 0-4, then it comes into ‘default’ section and executes the statement written in ‘default’.
If you run the above program you will get the same output as we got when we did it using if-else.
In the next section we will cover the second type of Control Statement - Iteration.

    

Loop Types in Java

0 comments:

Post a Comment

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you