Java’s Selection Statements | Controls Statement | Java Programming | Java For Beginner | If....Else and Switch.

Java supports two selection statements: if and switch. These statements allow you to control the
flow of your program’s execution based upon conditions known only during run time.

If Statements

The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:

   if (condition) statement 1;
   else statement 2;


Here, each statement may be a single statement or a compound statement enclosed in curly
braces (that is, a block). The condition is any expression that returns a Boolean value. The else clause is optional. 


Nested ifs

A nested if is an if statement that is the target of another if or else. Nested ifs are very common
in programming. When you nest ifs, the main thing to remember is that an else statement
always refers to the nearest if statement that is within the same block as the else and that is

not already associated with an else. Here is an example:


  if(i == 10)
  {
      if(j < 20) a = b;
      if(k > 100) c = d; // this if is
      else a = c; // associated with this else
  }
  else a = d; // this else refers to if(i == 10)



if-else-if Ladder

A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder. It looks like this:


   if(condition)
    statement;
   else if(condition)
    statement;
   else if(condition)
    statement;
   ...
   else
    statement;



The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.


Example of If.....Else


package demo;
import java.util.*;

public class Number
{
   public static void main(String[] args)
   {
      Scanner sc=new Scanner(System.in);

      System.out.println("Ënter the three Numbers");
      int a=sc.nextInt();
      int b=sc.nextInt();
      int c=sc.nextInt();

      if(a>b && a>c)
      {
         System.out.println(+a+"is Largest");
      }
      else if(b>a && b>c)
      {
         System.out.println(+b+"is Largest");
      }
      else
      {
         System.out.println(+c+"is Largest");
      }
   }
}



Switch statement

The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch
execution to different parts of your code based on the value of an expression. As such, it often
provides a better alternative than a large series of if-else-if statements. Here is the general form
of a switch statement:


  switch (expression)
  {
   case value1:
    // statement sequence
    break;
   case value2:
    // statement sequence
    break;
   ...
   case valueN:
    // statement sequence
    break;
   default:
    // default statement sequence
  }



The expression must be of type byte, short, int, or char; each of the values specified in the
case statements must be of a type compatible with the expression.Duplicate case
values are not allowed.

The break statement is used inside the switch to terminate a statement sequence. When
a break statement is encountered, execution branches to the first line of code that follows the
entire switch statement. This has the effect of “jumping out” of the switch.


Here is a example that uses a switch statement:


import java.util.Scanner;
public class Calculator
{
   public static void main(String[] args)
   {
      Scanner reader = new Scanner(System.in);
      System.out.print("Enter two numbers: ");
      double first = reader.nextDouble();
      double second = reader.nextDouble();
      System.out.print("Enter an operator (+, -, *, /): ");
      char operator = reader.next().charAt(0);
      double result;
      switch(operator)
      {
         case '+':
          result = first + second;
          break;
         case '-':
          result = first - second;
          break;
         case '*':
          result = first * second;
          break;
         case '/':
          result = first / second;
          break;
         default:
          System.out.printf("Error! operator is not correct");
return;
       }
       System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
   }
}




Post a Comment

2 Comments