Java Control Statements Selection Statements

                            Java Control Statement

A programming language uses control statements to cause the flow of execution to advance and branch based on changes to the state of program. Java program control statements can be put into following categories:
  • Selection

  • Iteration

  • Jump

           Java Control Statements Selection Statements

Java supports two selection statements if and switch. These allow you to control flow of program's execution based upon conditions given by the programmer.

IF:

The if statement is java's conditional branch statement. It can be used to route program execution through two different paths. The general form is given below:

if (condition) statement1;
else statement2;

Each statement may be a single statement or compound statement enclosed in curly brackets. The condition is any expression that returns a boolean value. The else clause is optional.
The if works like this: If the condition is true, then statement1 is executed. Otherwise, statement2 is executed. In no case both statements are executed.

/*
         This is a Simple Program Showing Use of if.
          Program developed by #Asad Hussain
         This file name is "Expif.java".
*/
import java.util.*;
public class Expif{
    public static void main(String args[]){
    int a, b;
    String c;
    a = 5; b = 7;
    if (a < b) c = "B is Greater";
    else c = "A is Greater";   
    System.out.println(""+c);
        }
}

Output of this Program:
Java Program Showing use of if Control Statement by Asad Hussain

Nested Ifs:

A nested if is an if statement that is target of another if or else. Nested ifs are very common in programming.

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 is associated with first if

The if-else-if ladder:

A common programming construct is the if-else-if ladder.
its general form is:

if (condition)
 statement;

else if (condition)
statement;

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

Switch:

The switch statement is Java's Multiway branch. It provides an easy way to dispatch execution to different paths of your code based on the value of an expression.
The general form:

switch (expression) {
case value1:
  statement ;
break;
case value0:
  statement ;
break;
.
.
.
case valueN:
  statement ;
break;
default:
}

Here is an exp of program showing use of Switch
/*
         This is a Simple Program Showing Use of Switch.
          Program developed by #Asad Hussain
         This file name is "Expswitch.java".
*/
import java.util.*;
public class Expswitch{
    public static void main(String args[]){
    for (int i = 0; i < 6; i++)
            switch(i){
                case 0:
                   System.out.println ("i is zero.");
                   break;
                case 1:
                   System.out.println ("i is one.");
                   break;
                case 2:
                   System.out.println ("i is two.");
                   break;
                case 3:
                   System.out.println ("i is three.");
                   break;
                case 4:
                   System.out.println ("i is four.");
                   break;
          
                default:
                   System.out.println ("i is greater than 4");

           }

    }


}

Output of this Program:
Java Program Showing use of Switch Control Statement by Asad Hussain

Nested Switch Statement:

You can use a switch as part of the statement sequence of an outer switch. This is called nested switch.

 Important Features of Switch Statement:

  • The switch statement differs from if in that switch can only test for equality, whereas if can evaluate any type of boolean expression.
  • No two case constants in the same switch can have identical values.
  • A switch statement is usually more efficient than a set of nested ifs.

Written By: Asad Hussain


Post a Comment

Previous Post Next Post