Fundamentals of computing 1 - Lecture title: Conditional expression

Calculating addition, subtract, multiply, divide of two integer numbers as like that: Input a: 6 Input b: 8 Input the operator (+,-,*,/): * 6 * 8 = 48

ppt39 trang | Chia sẻ: nguyenlam99 | Lượt xem: 913 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Fundamentals of computing 1 - Lecture title: Conditional expression, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Lecture Title: Conditional ExpressionFundamentals of Computing 1AgendaThe if statements The if/else statements The switch statements The if statementExecutes a block of statements only if a test is true if (test) { statement; ... statement; }Example: AgendaThe if statements The if/else statements The switch statements The if/else statementExecutes one block if a test is true, another if false if (test) { statement(s); } else { statement(s); }Example: Relational expressionsTests use relational operators:OperatorMeaningExampleValue==equals1 + 1 == 2true!=does not equal3.2 != 2.5truegreater than10 > 5true=greater than or equal to5.0 >= 5.0trueMisuse of ifScanner console = new Scanner(System.in);System.out.print("What percentage did you earn? ");int percent = console.nextInt();if (percent >= 90) { System.out.println("You got an A!");}if (percent >= 80) { System.out.println("You got a B!");}if (percent >= 70) { System.out.println("You got a C!");}if (percent >= 60) { System.out.println("You got a D!");}if (percent 0) { System.out.println("Positive"); } else if (x = 3 + 5 * (7 - 1)5 * 7 >= 3 + 5 * 635 >= 3 + 3035 >= 33trueRelational operators cannot be "chained" as in algebra.2 = y + z!(x b) { return a; } else { return b; }}Methods can return different values using if/elseWhichever path the code enters, it will return that value.Returning a value causes a method to immediately exit.All paths through the code must reach a return statement.All paths must returnpublic static int max(int a, int b) { if (a > b) { return a; } // Error: not all paths return a value}The following also does not compile:public static int max(int a, int b) { if (a > b) { return a; } else if (b >= a) { return b; }}if/else, return questionWrite a method quadrant that accepts a pair of real numbers x and y and returns the quadrant for that point: Example: quadrant(-4.2, 17.3) returns 2If the point falls directly on either axis, return 0. x+x-y+y-quadrant 1quadrant 2quadrant 3quadrant 4if/else, return answerpublic static int quadrant(double x, double y) { if (x > 0 && y > 0) { return 1; } else if (x 0) { return 2; } else if (x 0 && y = y) max = x;else max = y; max = (x >= y) ? x : y; equivalent toAgendaThe if statements The if/else statements The switch statements switch statement – syntax 1switch(dept){ case value_1: [statement_1; ]case value_2 : [statement_2; ]case value_3 : [statement_3; ].....case value_n : [statement_n; ][default: statement_n+1; ]}Note:The dept is an integer or a character expressionvalue_1, value_2, ..., value_n are integer or character constantsdept=value_1dept=value_2dept=value_3dept=value_nstatement n+1satatement 1statement 2 statement 3 statement ntruetruetruetruefalsefalsefalsefalseDiagramExercise 1:Input an integer number N (1>n; int n = kb.nextInt(); switch(n) { case 5: System.out.print(” Five \n“); break; case 4: System.out.print(“ Four \n“); break; case 3: System.out.print(“ Three \n“); break; case 2: System.out.print(”Two \n“); break; case 1: System.out.print(” One \n“); break; default : System.out.print(” No \n“); } }Exercise 3:Input an integer number n (0<n<=9). Checking n is whether prime number or not?HomeworkCalculating addition, subtract, multiply, divide of two integer numbers as like that:Input a: 4Input b: 3Input the operator (+,-,*,/): +4 + 3 = 7HomeworkCalculating addition, subtract, multiply, divide of two integer numbers as like that:Input a: 5Input b: 4Input the operator (+,-,*,/): -5 - 4 = 1HomeworkCalculating addition, subtract, multiply, divide of two integer numbers as like that:Input a: 6Input b: 8Input the operator (+,-,*,/): *6 * 8 = 48HomeworkCalculating addition, subtract, multiply, divide of two integer numbers as like that:Input a: 4Input b: 3Input the operator (+,-,*,/): /4 / 3 = 1.33What we have coveredThe if statements The if/else statements The switch statements

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

  • pptlecture_04_condional_expression_2437.ppt