0% found this document useful (0 votes)
2 views

Selection Statements in Java (2)

The document provides an overview of selection statements in Java, including constructs like if, if-else, nested if, and switch statements. It explains the syntax and usage of these statements, as well as the ternary operator as an alternative to if-else. Additionally, it discusses the dangling else problem and the characteristics of switch statements.

Uploaded by

dssamana1011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Selection Statements in Java (2)

The document provides an overview of selection statements in Java, including constructs like if, if-else, nested if, and switch statements. It explains the syntax and usage of these statements, as well as the ternary operator as an alternative to if-else. Additionally, it discusses the dangling else problem and the characteristics of switch statements.

Uploaded by

dssamana1011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Selection Statements in Java

Programming Constructs in java

• Sequential Construct.
• Selection/Decision Making/Conditional/Branching Construct.
→ if, if else, if else if, nested if and switch statements
• Looping/Iterative Construct.
→ for, while, do..while loops.
if statement in java
Syntax:-
if(test expression) { true block statements;}
Next statement;

Example:-
if(x==5) {System.out.println(“X value is 5”);}
System.out.println(“Hello”);
if else statement in java
Syntax:-
if(test expression) { if block statements;}
else
{ else block statements;}
Next statement;

Example:-
if(x%2==o) {System.out.println(“Even”);}
else {System.out.println(“Odd”);}
System.out.println(“Hello”);
Ternary operator in java→ ?:

Ternary Operator (Conditional Operator)


→ Operator with three operand.
Syntax:-
<variable> = test expression? Operand 2 : Operand 3;

Example:-
max = a>b ? a :b;
Ternary operator in java→ ?:

Ternary Operator → Operator used as an alternative of if..else


statement.
Example:-
if (a>b) max=a;
else max=b;
[OR]
max = a>b ? a :b;
Ternary operator in java→ ?:
if else statement Ternary expression
if (sales>10000)tax = 5/100.0 *sales; tax = (sales>10000) ? 5/100.0*sales:7/100.0*sales;
else tax = 7/100.0*sales;
if(a>b) System.out.println(a); System.out.println(a>b ? a :b);
else System.out.println(b);
String str; String str;
if (a>0)str = “Positive”; Str = (a>0) ? “Positive”: “Not a Positive”;
else str = “Not a Positive”;
if(a%2 = = 0) System.out.println(“Even”); System.out.println((a%2 = = 0) ? “Even”: “Odd”);
else System.out.println(“Odd”);
boolean b; boolean b;
if (age>=18) b=true; b = (age>=18)?true:false;
else b=false;
if else if statement in java
Syntax:-
if(test expression) { statement block;}
else if(test expression) {statement block;}
else
{statement block;}
Next statement;
Example:-
if(x>0) {System.out.println(“Positive”);}
else if (x<0){System.out.println(“Negative”);}
else System.out.println(“Zero”);
if else if ladder
Syntax:-
if(condition1) { statement block;}
else if(condition2) {statement block;}
else if(condition3) { statement block;}
else
{statement block;}

Next statement;
Nested if
Syntax:-
if(condition1)
{
if(condition2) {statement block;}
}
else
{
if(condition3) { statement block;}
}
Next statement;
Dangling else problem
It is the ambiguity that arises in the nested if statement , when the number of if
clause is more than the number of else clause. Java matches the else with the
nearest if clause. To solve this problem, curly brackets can be used according to the
user’s requirement.
For example:-
if(ch>=‘A’)
if(ch>=‘A’) {
if (ch<=‘Z’) if (ch<=‘Z’)
System.out.println(“Upper case”);
System.out.println(“Upper case”); }
else else
System.out.println(“Others”);
System.out.println(“Others”);
switch statement in java
It is the multi-way branching statement in java where the compiler tests the expression
against the list of values and executes the corresponding matching case. If there is no
matching case, the default is executed.
Syntax:-
switch(expression)
{ switch(Day)
case value1: statements; {
break; case 1: System.out.println(“Sunday”);
case value2:statements; break;
break; case 2: System.out.println(“Monday”);
case valuen:statements;
break;
break;
:
:
: :
default: statements default: System.out.println(“Invalid data”);
} }
switch statement in java
• There can be one or N number of case values for a switch expression.
• It checks only for the equality condition.
• The case value must be literal or constant. It doesn't allow variables.
• The case values must be unique. In case of duplicate value, it renders compile-
time error.
• The Java switch expression must be of byte, short, int, long, char and String.
• default label is optional.
• break is optional in each case.

You might also like