Top Banner
1 selection statements of Java
30
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 9 cm604.11

1

selection statements of Java

Page 2: 9 cm604.11

2

Objective

On completion of this period you would be able to

know

• Various selection statements available in Java

Page 3: 9 cm604.11

3

Recap

• In the previous class we have discussed

• Various types of operators

Page 4: 9 cm604.11

4

Selection statements of Java

• Java language supports the following selection

statements

• If statement

• Switch statement

• Conditional operator statement

Page 5: 9 cm604.11

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

Page 6: 9 cm604.11

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

Page 7: 9 cm604.11

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.

Page 8: 9 cm604.11

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

Page 9: 9 cm604.11

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

Page 10: 9 cm604.11

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");

}

Page 11: 9 cm604.11

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

Page 12: 9 cm604.11

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

Page 13: 9 cm604.11

13

if ( <boolean expression> ) {

} else {

}

Style Guide

if ( <boolean expression> )

{

}

else

{

}

Style 1Style 1

Style 2Style 2

Page 14: 9 cm604.11

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

Page 15: 9 cm604.11

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

Page 16: 9 cm604.11

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

Page 17: 9 cm604.11

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

Page 18: 9 cm604.11

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

Page 19: 9 cm604.11

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'.

Page 20: 9 cm604.11

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;

Page 21: 9 cm604.11

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>

}

Page 22: 9 cm604.11

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

Page 23: 9 cm604.11

23

Summary

• In this class we have discussed about various

selection statements

Page 24: 9 cm604.11

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

Page 25: 9 cm604.11

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

Page 26: 9 cm604.11

26

Quiz

2.Which of the following control statement requires break

a) If

b) If else

c) Switch

d) All of the above

Page 27: 9 cm604.11

27

Quiz

3.Default condition is always required in switch

statements .[ True/ False]

Page 28: 9 cm604.11

28

Quiz

3.Default condition is always required in switch

statements .[ True/ False]

Page 29: 9 cm604.11

29

Frequently Asked Questions

• Differentiate between switch and if else

statements

• List the various selection statements of Java

• Explain the various selection statements

Page 30: 9 cm604.11

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