Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

9 cm604.11

454 views

Published on

  • Be the first to comment

9 cm604.11

  1. 1. selection statements of Java 1
  2. 2. ObjectiveOn completion of this period you would be able to know• Various selection statements available in Java 2
  3. 3. Recap• In the previous class we have discussed • Various types of operators 3
  4. 4. Selection statements of Java• Java language supports the following selection statements• If statement• Switch statement• Conditional operator statement 4
  5. 5. The if Statement if ( <boolean expression> ) { <then block> } Boolean Boolean Expression Expression if ( testScore >= 95 ){Then Then System.out.println("You are a good student");BlockBlock } 5
  6. 6. Control Flow of if testScore >= true testScore >= 95? 95? System.out.println System.out.println false ("You are aagood student"); ("You are good student"); 6
  7. 7. The if-else Statementif (testScore < 50) { This statement is This statement is System. out. println ("You did not pass"); executed ififthe testScore executed the testScore is less than 50. is less than 50.} else { This statement is This statement is System. out. println ("You did pass"); executed ififthe testScore executed the testScore is 50 or higher. is 50 or higher.} 7
  8. 8. Syntax for the if-else Statement if ( <boolean expression> ) { <then block> } else { Boolean Boolean <else block> } Expression Expression if (testScore < 50) { System.out.println("You did not pass"); Then Then Block Block } else { System.out.println("You did pass");else Blockelse Block } 8
  9. 9. Control Flow false true testScore < 50 ?? testScore < 50 System.out.println System.out.printlnSystem.out.println System.out.println ("You did not ("You did not("You did pass"); ("You did pass"); pass"); pass"); 9
  10. 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"); } 10
  11. 11. Control Flow of Nested-if Statement true inner if false testScore >= 50 ?? testScore >= 50System.out.println System.out.println false studentAge < 10 studentAge < 10 true("You did not ("You did not ??pass"); pass"); System.out.println System.out.println System.out.println System.out.println ("You did aagreat ("You did great ("You did pass"); ("You did pass"); job"); job"); 11
  12. 12. Compound Statements• You have to use braces if the <then> or <else> block has multiple statements if (testScore < 70) { Then Block Then Block messageBox.show("You did not pass"); messageBox.show("Try harder next time"); } else { Else Block Else Block messageBox.show("You did pass"); messageBox.show("Keep up the good work"); } 12
  13. 13. Style Guideif ( <boolean expression> ) { …} else { Style 1 Style 1 …} if ( <boolean expression> ) { … Style 2 Style 2 } else { … } 13
  14. 14. if - else- ifif (score >= 85) { System.out.println(”Grade is A"); Test Grad} else { Score e if (score >= 75) { System.out.println(”Grade is B"); 85 ≤ score A } else { 75 ≤ score < B if (score >= 65) { 85 System.out.println(”Grade is C"); } else { 65 ≤ score < C if (score >= 50) { 75 System.out.println(”Grade is D"); 50 ≤ score < D } else { 65 System.out.println(”Grade is N"); } score < N } 50 }} 14
  15. 15. if - else- ifif (score >= 85) { Test Grad System.out.println(”Grade is A"); Score e} else if (score >= 75) { 85 ≤ score A System.out.println(”Grade is B"); 75 ≤ score < B} else if (score >= 65) { 85 System.out.println(”Grade is C"); 65 ≤ score < C} else if (score >= 50) { 75 System.out.println(”Grade is D"); 50 ≤ score < D} else { 65 System.out.println(”Grade is N"); score < N 50} 15
  16. 16. Matching else if (x < y) if (x < z) System.out.println("Hello"); else System.out.println("Good bye"); really meansif (x < y) { if (x < z) { System.out.println("Hello"); } else { System.out.println("Good bye"); }} 16
  17. 17. Matching else if (x < y) { if (x < z) System.out.println("Hello"); } else { System.out.println("Good bye"); } meansif (x < y) { if (x < z) { System.out.println("Hello"); }} else { System.out.println("Good bye"); 17}
  18. 18. Syntax for the switch Statement switch ( <arithmetic expression> ) { <case label 1> : <case body 1> … <case label n> : <case body n> } Arithmetic Expression Arithmetic Expression switch ( fanSpeed ) { case 1:Case CaseLabel System.out.println("Thats low");Label break; case 2: System.out.println("Thats medium"); Case Case break; Body Body case 3: System.out.println("Thats high"); break; } 18
  19. 19. The switch Statementchar standing;System.out.println("(F)reshman, (S)ophmore, (J)unior, s(E)nior : ");standing = SavitchIn.readLineNonwhiteChar();switch (standing) { case F: This statement This statement is executed ifif is executed System.out.println("Go to the Wellness Center"); the standing is the standing is break; equal to F. equal to F. case S: System.out.println("Go to the Cox Building"); break; case J: System.out.println("Go to Ashe"); break; case E: This statement This statement is executed ifif is executed System.out.println("Work it out yourself"); the standing is the standing is break; equal to E. equal to E. 19}
  20. 20. switch With break Statements true N == N ==switch ( N ) { 11?? xx= 10; = 10; case 1: x = 10; false break; break; break; true N == N == case 2: x = 20; 22?? xx= 20; = 20; break; false break; break; case 3: x = 30; true N == N == break; 33?? xx= 30; = 30;} false break; break; 20
  21. 21. The switch Statement with default switch ( <arithmetic expression> ) { <case label 1> : <case body 1> … <case label n> : <case body n> default: <default body> }switch ( binaryDigit ) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); break; default: System.out.println("Thats not a binary digit"); break; 21}
  22. 22. Switch With No break Statements true N == N == xx= 10;switch ( N ) { 11?? = 10; case 1: x = 10; false case 2: x = 20; true N == N == xx= 20; case 3: x = 30; 22?? = 20;} false true N == N == xx= 30; = 30; 33?? false 22
  23. 23. Summary• In this class we have discussed about various selection statements 23
  24. 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 24
  25. 25. Quiz1.The only relational operation that can be checked in switcha) less thanb) greater thanc) equalityd) all of the above 25
  26. 26. Quiz2.Which of the following control statement requires breaka) Ifb) If elsec) Switchd) All of the above 26
  27. 27. Quiz3.Default condition is always required in switch statements .[ True/ False] 27
  28. 28. Quiz3.Default condition is always required in switch statements .[ True/ False] 28
  29. 29. Frequently Asked Questions• Differentiate between switch and if else statements• List the various selection statements of Java• Explain the various selection statements 29
  30. 30. swings Struts jdbc hibernate home java previous question papersOCT/NOV-2012 QUESTION PAPER April / May 2012 c-09 October/ November-2011 c-09 April/ May 2011 c-09 April/ May 2011 c-05 Home 30

×