Bài giảng An Introduction to Computer Science Using Java - Chapter 4 Decision Making

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 4 Decision Making: Chapter 4 Decision MakingLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:discuss the use of decision making in computer programmingdescribe the use of the Java if and switch statementsdescribe the use of Boolean expressions in Java if statementsdiscuss the use of integer selector variables in Java switch statementsif StatementEnsures that a statement is executed only when a condition is trueConditions typically involve comparison of variables or quantities for equality or inequalityExample: if (age >= 18) out.println(“You are eligible to vote.”); Relational Operatorsif Statement with Optional elseAn if statement may have an optional else clause that will only be executed when the condition is falseExample: if (wages >=5. == !=6. ^7. &&8. ||Complicated Boolean Expressionsboolean isLeapYear = ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);// Assume a...

ppt29 trang | Chia sẻ: honghanh66 | Lượt xem: 792 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 4 Decision Making, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 4 Decision MakingLecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:discuss the use of decision making in computer programmingdescribe the use of the Java if and switch statementsdescribe the use of Boolean expressions in Java if statementsdiscuss the use of integer selector variables in Java switch statementsif StatementEnsures that a statement is executed only when a condition is trueConditions typically involve comparison of variables or quantities for equality or inequalityExample: if (age >= 18) out.println(“You are eligible to vote.”); Relational Operatorsif Statement with Optional elseAn if statement may have an optional else clause that will only be executed when the condition is falseExample: if (wages >=5. == !=6. ^7. &&8. ||Complicated Boolean Expressionsboolean isLeapYear = ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);// Assume all months have 31 daysdayNumber = (month - 1) * 31 + day;// Correct for months beyond Februaryif (month > 2) { dayNumber = dayNumber - ((4 * month +23) / 10); if (isLeapYear) // Correct for leap year dayNumber = dayNumber + 1;}Comparing StringsComparison is done by comparing strings character-by-character left to right, the first character that differs dictates which string is smaller lexicographicallyWhat if one string is a prefix of the other?The prefix is smaller than the longer stringHow do upper- and lower-case characters compare?If a case-sensitive comparison is used lower-case is always less than upper caseHow do special characters (e.g. %) compare?Decided by the character ASCII representationJava String Class Comparison Methodsswitch StatementUsed to accomplish multi-way branching based on the value of an integer selector variableExample: switch (numberOfPassengers) { case 0: out.println(“The Harley”); break; case 1: out.println(“The Dune Buggy”); break; default: out.println(“The Humvee”); }Why is break used in switch statements?Consider the code fragment below int i = 1; switch (i) { case 0: out.println(“0”); case 1: out.println(“1”); case 2: out.println(“2”); case 3: out.println(“3”); } out.println( );Without breaks the output is: 123Symbolic Constants in switch Statementsfinal int SUNDAY = 1, MONDAY = 2, TUESDAY = 3, WEDNESDAY = 4, THURSDAY = 5, FRIDAY = 6, SATURDAY = 7;ind d;...switch (d) { case SUNDAY: out.print(“Sunday”); break; case MONDAY: out.print(“Monday”); break; case TUESDAY: out.print(“Tuesday”); break; case WEDNESDAY: out.print(“Wednesday”); break; case THURSDAY: out.print(“Thursday”); break; case FRIDAY: out.print(“Friday”); break; case SUNDAY: out.print(“Sunday”); break;}Multiple case Labels in switch Statementsswitch (d) { case MONDAY: case WEDNESDAY: case FRIDAY: out.println(“C.S. meets at 9:00 today”); out.println(“Math meets at 10:00 today”); break; case TUESDAY: case THURSDAY: out.println(“English meets at 9:00 today”); out.println(“Chemistry meets at 10:00 today”); break; case SUNDAY: case Saturday out.println(“Enjoy the weekend”);}Comparing switch and if statementsswitch statementswitch (expression) { case value-1: statement-1; break; case value-2: statement-2; break; case value-i: statement-i; break; default: statement-(i+1);}if equivalentswitchValue = expression;if (switchValue == value-1) statement-1;else if (switchValue == value-2) statement-2;else if (switchValue == value-i) statement-i;else statement-(i+1);Building Classes with Multiple Methodspublic class Classname {// Author, date, explanation declarations of public variables public void methodName ( parameters ) { declarations of “local” variables executable statements with relevant comments } public void methodName ( parameters ) { declarations of “local” variables executable statements with relevant comments } }

Các file đính kèm theo tài liệu này:

  • pptchapter4_9077.ppt