Presentation is loading. Please wait.

Presentation is loading. Please wait.

1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !

Similar presentations


Presentation on theme: "1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !"— Presentation transcript:

1 1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !

2 1/30/2016IT 2752 Logical Operators (true || false) ((18 <= x) && (x <= 50)) ((18 <= x) || (x <= 50)) !(x = 5) (((x % 2) == 0) && ((x % 3) == 0)) || && ! Assume x = 10 true false

3 1/30/2016IT 2753 De Morgan’s law I am not a female student.  I am not female or I am not a student. I will not be in my office or in the lab.  I will not be in my office and will not be in the lab. !(A && B) is same as !A || !B !(A || B) is same as !A && !B

4 1/30/2016IT 2754 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F

5 1/30/2016IT 2755 The Syntax of if and if-else statements if ( condition ) { statement list; } if ( condition ) { statement list1; } else { statement list2; } Indentation indicates that the statements in the statement list are at the level next to the if-else statement. Reserved words A Boolean expression (logical expression). In Java, there are two possible values: true and false. A reserved word can’t be used as an identifier

6 1/30/2016IT 2756 Nested if/else statement if ( condition 1 ) { if ( condition 2 ) { statement list; } else { statement list; }; statement list; } else { statement list; } Indentation indicates the level of statements.

7 1/30/2016IT 2757 Disney Roller Coaster Ride Ride the roller coaster Give a Mickey Measure the kid’s height, h h < 4 Next Stop No Boy or Girl? Give a Mini Yes boygirl

8 1/30/2016IT 2758 Disney Ride in Program – OK n = Integer.parseInt( JOptionPane.showInputDialog(“How tall is the kid?”)); // IsBoy = true or false; if (h < 4) if (IsBoy) System.out.println(“Take a Mickey”); else System.out.println(“Take a Mini.”); else System.out.println(“You can ride!!”); System.out.println(“Go to the next stop.”); but not really a good idea; put { } to improve readability

9 1/30/2016IT 2759 Ambiguity I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top. I saw the little girl with a binocular on the mountain top.

10 1/30/2016IT 27510 Rule: else belongs to the nearest if that not yet has an else if (condition1) if (condition2) {.....; } else // not a kid {.....; } Indentation can’t help compiler

11 1/30/2016IT 27511 Boy Scout: Give a badge Is a kid? Welcome No Is a boy? Yes No Give a guidebook Yes No Parent?

12 1/30/2016IT 27512 Boy Scout: in program if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } else // not a kid { if (IsParent) { System.out.println(“Take a Guidebook”); } System.out.println(“Welcome, everybody!.”);

13 1/30/2016IT 27513 Boy Scout: wrong program if (IsKid) if (IsBoy) System.out.println(“Take a Badge”); else // not a kid if (IsParent) System.out.println(“Take a Guidebook”); System.out.println(“Welcome, everybody!.”); if (IsKid) { if (IsBoy) { System.out.println(“Take a Badge”); } else // not a boy { if (IsParent) System.out.println(“Take a Guidebook”); } System.out.println(“Welcome, everybody!.”);

14 1/30/2016IT 27514 Other forms of Nested if/else statements (Cascaded) I if (condition_1) statement_1; else if (condition_2) statement_2; else if (condition_3) statement_3; else if (condition_4) statement_4; else if (condition_5) statement_5; if (condition_1) statement_1; if (condition_2) statement_2; if (condition_3) statement_3; if (condition_4) statement_4; if (condition_5) statement_5; V.S.

15 1/30/2016IT 27515 Another form of Nested if/else statement (Cascaded) PointsGrade 600 --A 500 – 599B 400 – 499C 300 – 399D 0—299F if (points >= 600) System.out.println( “ A ” ); else if (points >= 500) System.out.println( “ B ” ); else if (points >= 400) System.out.println( “ C ” ); else if (points >= 300) System.out.println( “ D ” ); else System.out.println( “ F ” ); if (points >= 600) System.out.println( “ A ” ); if (points >= 500) System.out.println( “ B ” ); if (points >= 400) System.out.println( “ C ” ); if (points >= 300) System.out.println( “ D ” ); if (points < 300) System.out.println( “ F ” );

16 1/30/2016IT 27516 Other forms of Nested if/else statements (Cascaded) II if (condition_1) if (condition_2) if (condition_3) if (condition_4) statement_1; else statement_2; if ( condition_1 && condition_2 && condition_3 && condition_4 ) statement_1; else statement_2; =?=?

17 1/30/2016IT 27517 Switch vs. Cascaded if/else if (i == 1) statement_1; else if (i == 2) statement_2; else if (i == 3) statement_3; else if (i == 4) statement_4; else if (i == 5) statement_5; else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; break; case 3: statement_3; break; case 4: statement_4; break; case 5: statement_5; break; case 6: statement_6; break; }

18 1/30/2016IT 27518 Another form of Nested if/else statement (Cascaded) PointsGrade 600 --A 500 – 599B 400 – 499C 300 -- 399D 0--299F int g = (int) (point/100); switch (g) { case 6: System.out.println( “ A ” ); break; case 5: System.out.println( “ B ” ); break; case 4: System.out.println( “ C ” ); break; case 3: System.out.println( “ D ” ); break; default: System.out.println( “ F ” ); }

19 1/30/2016IT 27519 Switch vs. Cascaded if/else if (i == 1) statement_1; else if (i == 2) { statement_2; statement_3; } else if (i == 3) statement_3; else if (i == 4) { statement_4; statement_5 statement_6 } else if (i == 5) { statement_5; statement_6; } else if (i == 6) statement_6; switch (i) { case 1: statement_1; break; case 2: statement_2; case 3: statement_3; break; case 4: statement_4; case 5: statement_5; case 6: statement_6; }

20 1/30/2016IT 27520 Definite Loops vs indefinite loops isA definite loop is a loop that number of iterations is known before the loop begins, at least the upper bound is known. I.e., repeat the loop 100 times. is notAn indefinite loop is a loop that number of iterations is not known before the loop begins, at least the upper bound is known. I.e., repeat the loop until some condition reached. Precisely speaking, there is no definite loop in Java.

21 1/30/2016IT 27521 while loop Condition Statement list T F while (Condition) { Statement list } for loop Statement list < n = n for (???;???;???) { Statement list } repeat n times

22 1/30/2016IT 27522 The general format for a for loop for (Initialization_action; Condition; Condition_update) { statement_list; } int n = Integer.parseInt( JOptionPane.showInputDialog( “ input n ” )); int sum=0; for (i=0; i<=n; i++) { sum = sum + i; } System.out.println(“This answer is ” + sum); 12 3 Summation 1+2+3+...(n-1)+ n

23 1/30/2016IT 27523 for (Initialization_action; Condition; Condition_update) { statement_list; } int n,f=1; n = Integer.parseInt( JOptionPane.showInputDialog( “ input n ” ) ); if (n < 0) System.out.println(“No can do!!”); else for (i=2; i<=n; i++) { f = f*i; } System.out.println(“This answer is ” + f); 12 3 Factorial of n is n  (n-1)  (n-2) ...2  1

24 1/30/2016IT 27524 while Condition Statement list T F while (Condition) { Statement list } while (Condition) ; { Statement list }

25 1/30/2016IT 27525 Compare for and while int i; for (i=2; i<=n; i++) { f *= i; } i=2; while (i<=n) { f *= i; i++; } int i; for (i=0; i<n; i++) {.... } i=0; while (i<n) {.... i++;.... }

26 1/30/2016IT 27526 Another form of loop: Condition Statement list T F do loop do { Statement list } while (condition); This is a common patten E.g., IT CS student has to take IT168 until he/she gets a C or better. don’t forget “;” be careful

27 1/30/2016IT 27527 Another example of using do-loop String answer; do { answer = JOptionPane.showInputDialog( “ y or n? ” ); } while (!answer.equals( “ y ” ) && !answer.equals( “ n ” )); Ask the user to input y (yes) or n (no), where y and n are the only two valid inputs for the program to proceed. This way, we avoid a great chance of accidentally pressing the keyboard. String answer; answer = “ x ” ; while (!answer.equals( “ y ” ) && !answer.equals( “ n ” )) { answer = JOptionPane.showInputDialog( “ y or n? ” ); }

28 1/30/2016IT 27528 Primality Test A prime number is a positive integer that cannot be factorized, i.e., no numbers other that 1 and itself can divide it. is 893 a prime? Is (893 % 2 == 0) ? Is (893 % 3 == 0) ? Is (893 % 4 == 0) ? Is (893 % 5 == 0) ? Is (893 % 6 == 0) ?. Is (893 % 892 == 0) ? This test is silly but can do the job! We can write a loop to test these.

29 1/30/2016IT 27529 Primality Test int p,r,i; // r: remainder, i: factor i=1; do { i++; r = p % i; // r is the remainder of p divided by i } while (i < p && r != 0); if (i == p) System.out.println( “ a prime number. ” ); else { System.out.println( “ not a prime number. ” ); System.out.println( “ The least factor is ” +i); } do { p = Integer.parseInt( JOptionPane.showInputDialog( “ input an number:)); } while (p <= 1);

30 1/30/2016IT 27530 Break a loop int i = 2; do { r = p % i; if (r == 0) break; i++; } while (i < p); The break statement in a loop will force the program to jump out of the loop immediately. int i = 2; do { r = p % i; i++; } while (i < p && r != 0);

31 1/30/2016IT 27531 Break a loop import javax.swing.JOptionPane;.... public static void marryAsk() { String answer; do { answer = JOptionPane.showInputDialog( "Would you marry me? (y/n)"); if (answer.equals("Y") || answer.equals("y")) answer = JOptionPane.showInputDialog( "Really? (y/n)"); } while (!answer.equals("Y") && !answer.equals("y")); JOptionPane.showMessageDialog(null, "Great!!"); } if (answer.equals("F") || answer.equals("f"))break;

32 1/30/2016IT 27532 break and continue The break statement in a for/while loop will force the program to jump out of the for/while loop immediately. The continue statement in a for/while loop will force the program to update the loop condition and then check the condition immediately. for (Initialization_action; Condition; Condition_update) { statement_list; }

33 1/30/2016IT 27533 The Loops with multiple breaking points.... while (......) {.... if (....) break;.... if (....) break;.... if (....) break;.... if (....) break;..... }.... Condition Statement list T F Spaghetti code? Why spaghetti code is bad? Well, this is not spaghetti code!

34 1/30/2016IT 27534 The Loops with multiple breaking points This is Spaghetti code? This is not spaghetti code! All paths come to the same point Multiple-break-loop does not cause the trouble as the spaghetti code does

35 35 Euclid Algorithm Public int GCD(int a, int b) { int r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return (b); } 1/30/2016IT 275

36 My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2; if (a < 0) return -1; while (a - st*st != 0) { if (st*st > a) up = st; else down = st; st = (up+down)/2; } return st; } 1.Where can I improve the program? 2.Where is the potential problem? up down 0 a guess st real square root 1/30/201636IT 275

37 My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; `while (a - est > 0.0000001) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } Any problem? What is it? 1/30/201637IT 275

38 My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; while ((a - est > 0.0000001)||(a - est < -0.0000001)) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } Still wrong!! What is the roblem? 1/30/201638IT 275

39 My own square root function (my_sqrt) double my_sqrt(double a) { double up,down,st,est; if (a < 0) return -1; if ( a >= 1) { up=a; down=0;} else { up=1; down=a;} st = (up+down)/2; est = st*st; while ((a - est > 0.0000001) || (a - est < -0.0000001)) { if (est > a) up = st; else down = st; st = (up+down)/2; est = st*st; } return st; } 1/30/201639IT 275

40 1/30/2016IT 27540 Nested loops (loop in loop) int a = Integer.parseInt( JOptionPane.showInputDialog(“input a:”); int b = Integer.parseInt( JOptionPane.showInputDialog(“input b:”); for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { System.out.print(“*”); } System.out.print(“\n”); } ************* b a

41 1/30/2016IT 27541 Nested loops (2) int a = Integer.parseInt( JOptionPane.showInputDialog(“input a:”); int b = Integer.parseInt( JOptionPane.showInputDialog(“input b:”); for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) { if (j > i) break; System.out.print(“*”); } System.out.print(“\n”); } * ** *** **** b a

42 1/30/2016IT 27542 Java Blocks In Java and other C-like languages: a compound statement using { and } A compound statement also serves as a block: while (i < 0) { int c = i*i*i; p += c; q += c; i -= step; } for (int i=0;i<0; i++) { int c = i*i*i; p += c; q += c; i -= step; }

43 1/30/2016IT 27543 Can we do this? for (int i=0; i<n; i++) {.... } int i=2; Scope of red i {}{} Scope of blue i Yes!

44 1/30/2016IT 27544 Can we do this? for (int i=0; i<n; i++) {.... } i=2; Scope of red i {}{} blue i is undefined No!

45 1/30/2016IT 27545 Can we do this? for (int i=0; i<n; i++) {.... } int i=2; {}{} Scope of blue i No!


Download ppt "1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !"

Similar presentations


Ads by Google