Selection Statements

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Selection Statements:

 Selection statements are also known as branching statements or conditional or decision-


making statements.
 It is used to select part of a program to be executed based on condition.

Types Of selection Statements:

 If statement
 If –else Statement
 Nested if statement
 If-else-if ladder
 Switch statement

If Statement:

If the test expression is true then the statement is executed else the statement is not executed.

General Format:

If(test expression)

Block of Statements;

Statements
Example Program:

import java.util.Scanner;
class IfCode
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if((n % 2) == 0)
{
System.out.println("This is if-block");
System.out.println("The number is divisible by 2");
}
System.out.println("outter if-block");
}

Output:

Enter a number: 3
outter if-block

If-else Statement:
If the test expression is true then the if statements is executed otherwise else statements is
executed.

General format:
If (test expression)
{
True Block;
}
else
{
False Block;
}
Next Statements;
Example Program:

import java.util.Scanner;
class IfElseCode
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if((n % 5) == 0)
{
System.out.println("Condition is true");
}
else
{
System.out.println("Condition is false");
}
System.out.println("Outter i f-block");
}
Output:
Enter a number: 10
Condition is true

Nested If-else:
 The nested if statement in Java is a set of if conditions one within
another.
 The inner if conditions are only executed when the outer if condition
results is true; otherwise the corresponding else block is executed.
 The nested if condition can also be used with nested if..else statement.

General Format:
If(test condition)
{
If(Test condition)
{
Statement1;
}
else
{
Statement2
}
}
else
{
Statement3;
}
Statement-N;
Flow-chart

Example Program:
import java.util.Scanner;
public class NestedIfStatementTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n < 100)
{
System.out.println("\nGiven number is less than 100");
if (n % 5 == 0)
System.out.println("And it is a factor of 5");
else
System.out.println("Not a factor of 5");
}
else
System.out.println("Given number is greater than 100")
System.out.println("\outer of if-block");
}
}
If-else-if ladder:
 We use the if-else-if statement when we want to check multiple
conditions and want to execute a one statement depending on these
conditions
 The if statements are executed from the top down.
 As soon as one of the conditions 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.

Syntax:

if (condition)
{
statement 1;
}
else if (condition)
{
statement 2;
}
.
.
Else
{
statement;
}
Flow-char
Example program:
import java.util.Scanner;
class IfElseIfCode
{
public static void main(String[] args)
{
int n1, n2, n3;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any three number: ");
n1 = sc.nextInt();
n2 = sc.nextInt();
n3 = sc.nextInt();
if( n1>=n2 && n1>=n3)
System.out.println("\n largest number" + n1) ;
else if (n2>=n1 && n2>=n3)
System.out.println("\nlargest number " + n2) ;
else
System.out.println("\nlargest number" + n3) ;
System.out.println("\outter if-block");
}
}

Switch Statement:
 The switch statement selects one of many code blocks to be executed.
 The switch statement works with byte, short, int, long, enum types, String
 Each case statement can have a break statement
 The expression is evaluated once and compared with the values of each case.

 If expression matches with value1, the code of case value1 is executed. Similarly,
the code of case value2 is executed if expression matches with value2.
 If there is no match, the code of the default case is executed.

Syntax:
Switch (expression)
{
Case value1:
Statement-1;
break;
Case value2:
Statement-2;
break;
.
.
.
Case valueN
Statement-N
Default:
Default Statements;
}

Flow Chart:
Example Program:
class SwitchMonth
{
public static void main(String[] args)
{
int month=7;
String monthString="";
switch(month)
{
case 1:
monthString="January";
break;
case 2:
monthString="February";
break;
case 3:
monthString="March";
break;
case 4:
monthString="April";
break;
case 5:
monthString=" May";
break;
case 6:
monthString="June";
break;
case 7:
monthString="July";
break;
case 8:
monthString="August";
break;
case 9:
monthString=" September";
break;
case 10:
monthString="October";
break;
case 11:
monthString="November";
break;
case 12:
monthString="December";
break;
default:
System.out.println("Invalid Month!");
}
System.out.println(monthString);
}
}
Ternary Operator:
 The ternary operator is a conditional operator
 A ternary operator evaluates the test condition and executes a block of code based
on the result of the condition.

Syntax:

Expression-1(Condition) ? expression-2 : expression-3

 Here, condition is evaluated and


 if condition is true, expression2 is executed.
 And, if condition is false, expression-3 is executed.
 The ternary operator takes 3 operands (condition, expression1,
and expression2).

Sample Program
class TernaryOperatorExample
{
public static void main(String args[])
{
int x, y;
x = 20;
y = (x == 1) ? 61: 90;
System.out.println("Value of y is: " + y);
y = (x == 20) ? 61: 90;
System.out.println("Value of y is: " + y);
}
}

You might also like