1 / 42

Expressions, Operators, Program Control

Expressions, Operators, Program Control. Week 3. Expression. An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value. Correct or Not Correct?. 25 2 ( a – b ) A - b / c + D ( (x + y) / z) / (a - b) 25 - value -sum + partial

bill
Download Presentation

Expressions, Operators, Program Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Expressions, Operators, Program Control Week 3

  2. Expression • An expression is a combination of literals, operators, variable names, and parentheses used to calculate a value.

  3. Correct or Not Correct? 25 2 ( a – b ) A - b / c + D ( (x + y) / z) / (a - b) 25 - value -sum + partial ( (m - n) + (w - x - z) / (p % q) 201ids - x

  4. Arithmetic Operators • Addition: + • Subtraction: - • Multiplication: * • Division: / • Reminder: % • Integer division (c = a/b) • aand b are integers, c will be an integer; 7/4 = 1 • As long as a, b has at least one real, then c will be a real. 7/4.0 = 1.75

  5. Reminder (%) • Positive case • 13%5 = 3 • Reminder with negative integers • Perform the operation as if both operands are positive; • If the left operand is negative, then make the result negative; • If the left operand is positive, the make the result positive; • Ignore the sign of the right operand in all cases.

  6. Increment and Decrement • counter = counter + 1; • Take the old value of counter from memory (right side); • Add one to counter; • Assign the new value to counter in memory (left side). • Incrementing and decrementing operators. • Adding 1 to a variable: ++ • X++; • ++X; • Subtracting 1 to a variable: -- • X--; • --X;

  7. Increment • The difference between Y = X++ and Y = ++X Y = X++; (e.g. X = 5;) • The value of Y is the value of expression X++ which is the old value of X, before 1 is added. • X = 6; • Y = 5; Y = ++X; (e.g. X = 5;) • The value of Y is the value of expression ++X which is the new value of X, after 1 is added. • X = 6; • Y = 6;

  8. Decrement • The difference between Y = X-- and Y = --X Y = X--; (e.g. X = 5;) • The value of Y is the value of expression X-- which is the old value of X, before 1 is subtracted. • X = 4; • Y = 5; Y = --X; (e.g. X = 5;) • The value of Y is the value of expression --X which is the new value of X, after 1 is subtracted. • X = 4; • Y = 4;

  9. Example X = 5; Y = X--; X = 4; Y = 5; X = 5; Y = ++X; X = 6; Y = 6;

  10. Relational Operators The return type of expressions using relational operators is boolean.

  11. Relational Operators • They can be used to compare any numeric values. • For character, it’s defined according the Unicode value (A: 65, a: 97, …). • ‘A’ < ‘a’ • ==, != can also be used to compare two boolean values. booleansameSign; sameSign= ((x > 0) == (y > 0)); • ==, !=: better not use for Strings. • String is a Object type, using their specific methods.

  12. Boolean Operators • && : and • The value is true if both values are true. The value is false if either value is false. • || : or • The value is true if either value is true. The value is false if both values are false. • ! : not • Will reverse the value.

  13. Boolean Operators • && • || • !

  14. Short Circuited Version • Operand 1 && Operand 2 • If operand 1 is false, operand 2 doesn’t have to be evaluated. • E.g. X = 0; Y = 1; (X != 0) && (Y/X); The division by zero is avoided, because X != 0 is false. • Operand 1 || Operand 2 • If operand 1 is true, operand 2 doesn’t have to be evaluated. • E.g. X = 0; Y = 1; (X == 0) || (Y/X); The division by zero is avoided, because X == 0 is true.

  15. Conditional Operators • ⟨boolean-expression⟩ ? ⟨expression1⟩ : ⟨expression2⟩ • If the boolean-expression is true, it evaluates expression 1, otherwise expression 2. • E.g. next = (N % 2 == 0) ? (N/2) : (3*N+1); next = N/2 if N iseven; next = 3*N+1 is N isodd;

  16. Automatic Type Casts • Variable = expression; • Usually, the type of variable should be the same as the type of expression. • However, automatically conversion happens to match the expression type to the variable type. • Conversion order: {byte, short, int, long, float, double} • occurs earlier in this list can be converted automatically to a value that occurs later. • No information lose.

  17. Example • int A; • double X; • short B; • A = 17; • X = A; // OK; A is converted to a double • B = A; // illegal; no automatic conversion // from int to short

  18. If either (or both) of the operands of the + operator is a string, the other is automatically cast to a string. • System.out.println("1 + 2 = " + 1 + 2); // 1 + 2 = 12 • System.out.println("1 + 2 = " + (1 + 2)); // 1 + 2 = 3

  19. Explicit Type Casts • Force conversion. • Atype cast is indicated by putting a type name, in parentheses, in front of the value you want to convert. • E.g. int A = 17; short B; B = (short)A; // OK; A is explicitly type cast // to a value of type short

  20. Explicit Type Casts • Some information may lose. Why??? • E.g. int A = 100,000; short B; B = (short)A; A: int (4 bytes) B: short (2 bytes) drop first two bytes 10100000 00000000 00000001 10000110 10100000 10000110 B = -31072

  21. Example • Math.random(); // return real number between 0 and 1; • What if we want to get a random integer from 1 to10 (inclusive)? • int X = (int)(10*Math.random() )+ 1; • Math.random()  0 =< a real number < 1; • 10*Math.random()  0 =< a real number < 10; • (int)(10*Math.random())  0 =< an integer < 10; • (int)(10*Math.random()) + 1  1 =< an integer <= 10;

  22. Type Casts on Char • The numeric value of a char is its Unicode code number. For example, (char)97 is ’a’, and (int)’+’ is 43. • char x = ‘a’; int y = x; // y = 97; char to int is automatically converted; • int x = 97; char y = (char)x; // y = ‘a’; int to char needs to an explicit conversion.

  23. Assignment Operators x+=y; // same as x=x+y; x-=y; //same as: x=x-y; x*=y; //same as: x=x*y;x/=y; //same as: x=x/y;x%=y; //same as: x=x%y; (for integers x and y) q&&=p; //same as: q=q&&p; (for booleans q and p)

  24. Type Conversion of Strings • Java build-in methods: • String “10”  integer 10 Integer.parseInt(“10”); • String “15.3e-2”  double 0.153 Double.parseDouble(“15.3e-2”);

  25. Operator Precedence • List in order from highest precedence (evaluated first) to lowest precedence (evaluated last): • Operators on the same line have same precedence

  26. Operator Precedence • unary operators and assignment operators are evaluated right-to-left, while the remaining operators are evaluated left-to-right. • A = B = C; means A = (B = C); • A * B / C; means (A * B) / C;

  27. Program Controls • 6 types of control structures in Java. • Blocks • while loop • if statement • do…while loop • for loop • switch

  28. Blocks • It groups a sequence of statements into a single statement. { <statements> } • Empty block { }

  29. Example

  30. while Loop • A while loop will repeat over and over , so long as the specified condition remains true. while(<boolean-expression>) <statement> while(<boolean-expression>){ <statements> } Continuation condition Body of the loop

  31. Example intnumber; // The number to be printed. number = 1; // Start with 1. while(number<6) { //Keep going as long as number is<6. System.out.println(number); number = number + 1; // Go on to the next number. } System.out.println("Done!"); ------------------------------ 1 2 3 4 5 Done!

  32. if Statement if ( ⟨boolean-expression ⟩ ) ⟨statement 1⟩ else ⟨statement 2⟩ ---------------------------------- If the boolean-expression true, it excutes statement 1, otherwise statement 2.

  33. if Statement if ( ⟨boolean-expression ⟩ ) ⟨statement⟩ if ( ⟨boolean-expression ⟩ ) { ⟨statements⟩ } if ( ⟨boolean-expression ⟩ ) { ⟨statements⟩ } else { ⟨statements⟩ }

  34. The Dangling else Problem if (⟨boolean-expression ⟩) ⟨statement-1 ⟩ else ⟨statement-2 ⟩ • Either statement-1 or statement-2 (or both) can itself be an if statement. • Problem arises if statement-1 has no else part.

  35. int x = 0; int y = 5; if ( x > 0 ) if (y > 0) System.out.println(“x>0 and y>0”); else System.out.println(“x<=0”); Based on the closer attach principle, the computer will explain it as: if ( x > 0 ) if (y > 0) System.out.println(“x>0 and y>0”); else System.out.println(“x<=0”); Output will be nothing

  36. You can force the else part to be the second half of the if(x>0) statement using parenthesis. if ( x > 0 ) { if (y > 0) System.out.println(“x>0 and y>0”); } else System.out.println(“x<=0”); Output will be: x<=0

  37. if…else if construction if (⟨boolean-expression-1 ⟩) ⟨statement-1 ⟩ else if (⟨boolean-expression-2 ⟩) ⟨statement-2 ⟩ else ⟨statement-3 ⟩ More generally: if (⟨boolean-expression-1 ⟩) ⟨statement-1 ⟩ else if (⟨boolean-expression-2 ⟩) ⟨statement-2 ⟩ else if (⟨boolean-expression-3 ⟩) ⟨statement-3 ⟩ . . // (more cases) . else if (⟨boolean-expression-N ⟩) ⟨statement-N ⟩ else ⟨statement-N+1⟩

  38. Example int score = 88; System.out.print(“Your grade is ”); if(score > 90) System.out.println(‘A’); else if(score > 80) System.out.println(‘B’); else if(score > 70) System.out.println(‘C’); else System.out.println(“too bad”); Your grade is B

  39. do…while do { ⟨statements ⟩}while ( ⟨boolean-expression ⟩ ); • The only difference from while loop is that the statements will be executed >= 1 times.

  40. Variations

  41. for Loop for ( ⟨initialization ⟩; ⟨continuation-condition ⟩; ⟨update ⟩ ) { ⟨statements ⟩ } Example -- counting loop for(⟨variable⟩=⟨min⟩; ⟨variable⟩<=⟨max⟩;⟨variable⟩++) { ⟨statements ⟩ }

  42. Examples for(N=1; N<=10; N++) System.out.println( N ); // output: 1, 2, 3,…,10. Each number is a line; for(N=0; N<10; N++) System.out.println( N ); // output: 0, 1, 2,…,9. Each number is a line; for (N = 2; N <= 10; N++) { if ( N % 2 == 0 ) // is N even System.out.println( N ); } // output even numbers from 2 to 10. Each number is a line.

More Related