Friday, 29 May 2015

Java Selection Statements (Decision Control Structure)

Java supports the following selection statements:

  • simple if Statement
  • if-else Statement
  • nested-if Statement
  • if-else-if Ladder
  • switch Statement

The syntax of these statements is same as in C/C++ so we will be discussing in details only if there is some difference from C/C++.

Simple if Statement

The syntax of the simple if statement is:

if (condition)
Statement;

if-else Statement

The syntax of the if-else statement is

if(condition)
   Statement1;
else
   Statement2;

Example: The following program illustrates the use of simple if statement as well as if-else statement. The program reads elements in an array of type int, followed by the element to be searched in the array using the linear search method. The program then displays the suitable message according to the search result.

import java.util.*;

class LinearSearch

{ public static void main(String args[]) throws IOException

{ int n, x, a[]; boolean found = false;

String number;

Buffered Reader in

System.out.print("Enter the number of elements in the array: ");

number = in.readLine();

n = Integer.parseInt(number);

a = new int[n];

for(int i = 0; i < n; i++) // read the array elements

{ number = in.readLine(); // enter only one element in one line

}

System.out.print("Enter the element to be searched in the array: ");

number = in.readLine();

x = Integer.parseInt(number);

for(int i = 0; i < n; i++) // search the specified element

{

}

if(found) // if-else statement

else

}

}

nested-if Statement

Any type of if statement may appear in the if or else part of another if statement. The rules for nesting are same as in C/C++. The following example illustrates the use of nested-if statement.

Example: The following program finds the largest among three given numbers.

import java.util.*;

class LargestOfThree

{ public static void main(String args[]) throws IOException

{ int a,b,c,max;  String number;

Buffered Reader in

System.out.print("Enter the first number : ");

number = in.readLine(); a = Integer.parseInt(number);

System.out.print("Enter the second number : ");

number = in.readLine(); b = Integer.parseInt(number);

System.out.print("Enter the third number : ");

number = in.readLine(); c = Integer.parseInt(number);

if(a > b)

else

System.out.println("Maximum among "+a+","+b+","+c+" is:"+max);

}

}

if-else-if Ladder

The syntax of the if-else-if ladder statement is as follows:

if(condition)
  Statement;
else if (condition)
  Statement;
else if (condition)
  Statement;
else
  Statement;

Example: The following program demonstrates the use of if-else-if ladder. The program reads

the weekday as an integer and displays corresponding weekday.

import java.util.*;

class DisplayWeekDay

{ public static void main(String args[]) throws IOException

{ int weekDay;

Buffered Reader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the day of week (1 to 7) : ");

number = in.readLine(); weekDay = Integer.parseInt(number);

if (weekDay == 1)

System.out.println("Sunday");

else if(weekDay == 2)

System.out.println("Monday");

else if(weekDay == 3)

System.out.println("Tuesday");

else if(weekDay == 4)

System.out.println("Wednesday");

else if(weekDay == 5)

System.out.println("Thursday");

else if(weekDay == 6)

System.out.println("Friday");

else if(weekDay == 7)

System.out.println("Saturday");

else

System.out.println("Valid values are from 1 to 7");

}

}

switch Statement

The switch statement is a multi-way branch statement. Its syntax is as follows:

switch(expression)

{ case label-1: Statement Sequence

case label-2 : Statement Sequence

[break;]

[break;]

case label-n: Statement Sequence

default: Statement Sequence

[break;]

[break;]

}

The syntax is same as in C/C++, with the following differences:

(i) In Java, switch expression can be any integer expression except long i.e. the type of the switch expression can be byte, short, char or int. But the Java’s int is of 4 bytes which is same as size of long in C/C++.

(ii) The case labels are constant expressions as in C/C++ but the values of the case labels must be in the range of the type of the switch expression otherwise the program will not compile. For example, if switch expression is of type byte then the valid values for case labels are from –128 to 127.

Example: The following program demonstrates the use of switch statement. The program reads the weekday as an integer and displays the corresponding weekday in character form.

import java.util.*;

class DisplayWeekDay1

{ public static void main(String args[]) throws IOException

{ int weekDay;

String number;

Buffered Reader in

= new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the day of week (1 to 7) : ");

number = in.readLine();

weekDay = Integer.parseInt(number);

switch(weekDay)

{

case 1: System.out.println("Sunday");

case 2: System.out.println("Monday");

case 3: System.out.println("Tuesday");

case 4: System.out.println("Wednesday");

case 5: System.out.println("Thursday");

case 6: System.out.println("Friday");

break;

break;

break;

break;

break;

break;

case 7: System.out.println("Saturday");

break;

default:

System.out.println("Valid values are from 1 to 7");

}
}
}

No comments:

Post a Comment