12
IMPLEMENTING MULTIPLE SELECTIONS Chapter 4.4:

Chapter 4.4

  • Upload
    sotlsoc

  • View
    68

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 4.4

IMPLEMENTING MULTIPLE SELECTIONS

Chapter 4.4:

Page 2: Chapter 4.4

Multiple Selection (nested if) Syntax:

if (expression1) statement1

elseif (expression2) statement2else statement3

Page 3: Chapter 4.4

Java code (multiple selection)if (a>=1){

System.out.println ("The number you enter is :" + a); System.out.println ("You enter the positive number");

}else if (a<0){

System.out.println ("The number you enter is :" + a); System.out.println ("You enter the negative number");

}else {

System.out.println ("The number you enter is :" + a); System.out.println ("You enter the zero number");

}

Page 4: Chapter 4.4

Output

Enter the number : 15The number you enter is :15You enter the positive number

Enter the number : -15The number you enter is :-15You enter the negative number Enter the number : 0The number you enter is :0You enter the zero number 

 

Page 5: Chapter 4.4

Multiple Selections

Example

The grading scheme for a course is given as below:

Mark Grade

90 - 100 A

80 – 89 B

70 – 79 C

60 – 69 D

0 - 59 F

Read a mark & determine the grade.

Page 6: Chapter 4.4

Multiple Selectionsif (mark >= 90)

grade = ‘A’;

else if (mark >= 80)

grade = ‘B’;

else if (mark >= 70)

grade = ‘C’;

else if (mark >= 60)

grade = ‘D’;

else

grade = ‘F’;

Page 7: Chapter 4.4

Equivalent code with series of if statements

if ((mark >= 90) && (mark <=100))

grade = ‘A’;

if ((mark >= 80) && (mark >= 89))

grade = ‘B’;

if ((mark >= 70) && (mark >= 79))

grade = ‘C’;

if ((mark >= 60) && (mark >= 69))

grade = ‘D’;

if ((mark >= 0) && (mark >= 59))

grade = ‘F’;

Page 8: Chapter 4.4

switch Structures (multiple selection)

Expression is also known as selector.

Value can only be integral.

switch (expression){case value1: statements1

break;case value2: statements2

break; ...case valuen: statementsn

break;default: statements}

If expressionmatches value2,control jumpsto here

Page 9: Chapter 4.4

switch Structures

Page 10: Chapter 4.4

The switch Statement Often a break statement is used as the last

statement in each case's statement list

A break statement causes control to transfer to the end of the switch statement

If a break statement is not used, the flow of control will continue into the next case

Page 11: Chapter 4.4

Control flow of switch statement with and without the break statements

Page 12: Chapter 4.4

Switch/Break Examples

int m = 2;switch (m){ case 1 : System.out.println(“m=1”); break; case 2 : System.out.println(“m=2”); break; case 3 : System.out.println(“m=3”); break; default: System.out.println(“default”);}

int m = 2;switch (m){ case 1 : System.out.println(“m=1”); break; case 2 : System.out.println(“m=2”); break; case 3 : System.out.println(“m=3”); break; default: System.out.println(“default”);}

Output: m=2

char ch = ‘b’;switch (ch){ case ‘a’ : System.out.println(“ch=a”);

case ‘b’ : System.out.println(“ch=b”);

case ‘c’ : System.out.println(“ch=c”);

default: System.out.println(“default”);}

char ch = ‘b’;switch (ch){ case ‘a’ : System.out.println(“ch=a”);

case ‘b’ : System.out.println(“ch=b”);

case ‘c’ : System.out.println(“ch=c”);

default: System.out.println(“default”);}

Output: ch=b ch=c default