Upload
myrajendra
View
124
Download
0
Tags:
Embed Size (px)
Citation preview
1
selection statements of Java
2
Objective
On completion of this period you would be able to
know
• Various selection statements available in Java
3
Recap
• In the previous class we have discussed
• Various types of operators
4
Selection statements of Java
• Java language supports the following selection
statements
• If statement
• Switch statement
• Conditional operator statement
5
The if Statement
if ( testScore >= 95 ) {
System.out.println("You are a good student");
}
if ( <boolean expression> ) {
<then block>
}
Then Block
Then Block
Boolean Expression
Boolean Expression
6
Control Flow of if
testScore >= 95?
testScore >= 95?
falseSystem.out.println("You are a good student");
System.out.println("You are a good student");
true
7
The if-else Statement
if (testScore < 50) {
System. out. println ("You did not pass");
} else {
System. out. println ("You did pass");
}
This statement is executed if the testScore is 50 or higher.
This statement is executed if the testScore is 50 or higher.
This statement is executed if the testScore is less than 50.
This statement is executed if the testScore is less than 50.
8
if (testScore < 50) {
System.out.println("You did not pass");
} else {
System.out.println("You did pass");
}
Syntax for the if-else Statementif ( <boolean expression> ) {
<then block>
} else {
<else block> }
Then Block
Then Block
else Blockelse Block
Boolean Expression
Boolean Expression
9
Control Flow
System.out.println("You did pass");
System.out.println("You did pass");
falsetestScore < 50 ?testScore < 50 ?
System.out.println("You did not pass");
System.out.println("You did not pass");
true
10
The Nested-if Statement• The then and else block of an if statement can contain any
valid statements, including other if statements. An if statement
containing another if statement is called a nested-if statement
if (testScore >= 50) {
if (studentAge < 10) {
System.out.println("You did a great job");
} else {
System.out.println("You did pass");
}
} else { //test score < 70
System.out.println("You did not pass");
}
11
Control Flow of Nested-if Statement
System.out.println("You did not pass");
System.out.println("You did not pass");
false inner if
System.out.println("You did pass");
System.out.println("You did pass");
false
testScore >= 50 ?testScore >= 50 ?true
studentAge < 10 ?
studentAge < 10 ?
System.out.println("You did a great
job");
System.out.println("You did a great
job");
true
12
if (testScore < 70)
{
messageBox.show("You did not pass");
messageBox.show("Try harder next time");
}
else
{
messageBox.show("You did pass");
messageBox.show("Keep up the good work");
}
Compound Statements
• You have to use braces if the <then> or <else> block has multiple statements
Then BlockThen Block
Else BlockElse Block
13
if ( <boolean expression> ) {
…
} else {
…
}
Style Guide
if ( <boolean expression> )
{
…
}
else
{
…
}
Style 1Style 1
Style 2Style 2
14
if - else- if if (score >= 85) {
System.out.println(”Grade is A");
} else {
if (score >= 75) {
System.out.println(”Grade is B");
} else {
if (score >= 65) {
System.out.println(”Grade is C");
} else {
if (score >= 50) {
System.out.println(”Grade is D");
} else {
System.out.println(”Grade is N");
}
}
}
}
Test Score
Grade
85 score A75 score 85
B
65 score 75
C
50 score 65
D
score 50
N
15
if - else- if if (score >= 85) {
System.out.println(”Grade is A");
} else if (score >= 75) {
System.out.println(”Grade is B");
} else if (score >= 65) {
System.out.println(”Grade is C");
} else if (score >= 50) {
System.out.println(”Grade is D");
} else {
System.out.println(”Grade is N");
}
Test Score
Grade
85 score A75 score 85
B
65 score 75
C
50 score 65
D
score 50
N
16
Matching else
if (x < y)
if (x < z)
System.out.println("Hello");
else
System.out.println("Good bye");
if (x < y) {
if (x < z) {
System.out.println("Hello");
} else {
System.out.println("Good bye");
}
}
really means
17
Matching else
if (x < y) {
if (x < z)
System.out.println("Hello");
} else {
System.out.println("Good bye");
}
if (x < y) {
if (x < z) {
System.out.println("Hello");
}
} else {
System.out.println("Good bye");
}
means
18
Syntax for the switch Statement
switch ( fanSpeed ) {
case 1:
System.out.println("That's low");
break;
case 2:
System.out.println("That's medium");
break;
case 3:
System.out.println("That's high");
break;
}
switch ( <arithmetic expression> ) {
<case label 1> : <case body 1>
…
<case label n> : <case body n>
}
Case Body
Case Body
Arithmetic ExpressionArithmetic Expression
Case Label
Case Label
19
The switch Statementchar standing;
System.out.println("(F)reshman, (S)ophmore, (J)unior, s(E)nior : ");
standing = SavitchIn.readLineNonwhiteChar();
switch (standing) {
case 'F':
System.out.println("Go to the Wellness Center");
break;
case 'S':
System.out.println("Go to the Cox Building");
break;
case 'J':
System.out.println("Go to Ashe");
break;
case 'E':
System.out.println("Work it out yourself");
break;
}
This statement is executed if the standing is equal to 'F'.
This statement is executed if the standing is equal to 'F'.
This statement is executed if the standing is equal to 'E'.
This statement is executed if the standing is equal to 'E'.
20
switch With break Statements
switch ( N ) {
case 1: x = 10;
break;
case 2: x = 20;
break;
case 3: x = 30;
break;
}
x = 10;x = 10;
false
trueN == 1 ?
N == 1 ?
x = 20;x = 20;
x = 30;x = 30;
N == 2 ?
N == 2 ?
N == 3 ?
N == 3 ?
false
false
true
true
break;break;
break;break;
break;break;
21
The switch Statement with default
switch ( binaryDigit ) {
case 0:
System.out.println("zero"); break;
case 1:
System.out.println("one"); break;
default:
System.out.println("That's not a binary digit"); break;
}
switch ( <arithmetic expression> ) {<case label 1> : <case body 1>…<case label n> : <case body n>default: <default body>
}
22
Switch With No break Statements
switch ( N ) {
case 1: x = 10;
case 2: x = 20;
case 3: x = 30;
}
x = 10;x = 10;
false
trueN == 1 ?
N == 1 ?
x = 20;x = 20;
x = 30;x = 30;
N == 2 ?
N == 2 ?
N == 3 ?
N == 3 ?
false
false
true
true
23
Summary
• In this class we have discussed about various
selection statements
24
Assignment
• Write a Java program to find whether the given
year is leap or not
• Write a Java program to find the largest of three
numbers
• Write a Java program illustrating the functioning
of switch statement
25
Quiz
1.The only relational operation that can be checked in switch
a) less than
b) greater than
c) equality
d) all of the above
26
Quiz
2.Which of the following control statement requires break
a) If
b) If else
c) Switch
d) All of the above
27
Quiz
3.Default condition is always required in switch
statements .[ True/ False]
28
Quiz
3.Default condition is always required in switch
statements .[ True/ False]
29
Frequently Asked Questions
• Differentiate between switch and if else
statements
• List the various selection statements of Java
• Explain the various selection statements
swingsStrutsjdbc
hibernatehome
java previous question papers OCT/NOV-2012 QUESTION PAPER
April / May 2012 c-09October/ November-2011 c-09
April/ May 2011 c-09April/ May 2011 c-05
Home30