420 likes | 607 Views
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
E N D
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 ( (m - n) + (w - x - z) / (p % q) 201ids - x
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
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.
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;
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;
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;
Example X = 5; Y = X--; X = 4; Y = 5; X = 5; Y = ++X; X = 6; Y = 6;
Relational Operators The return type of expressions using relational operators is boolean.
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.
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.
Boolean Operators • && • || • !
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.
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;
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.
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
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
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
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
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;
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.
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)
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”);
Operator Precedence • List in order from highest precedence (evaluated first) to lowest precedence (evaluated last): • Operators on the same line have same precedence
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;
Program Controls • 6 types of control structures in Java. • Blocks • while loop • if statement • do…while loop • for loop • switch
Blocks • It groups a sequence of statements into a single statement. { <statements> } • Empty block { }
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
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!
if Statement if ( ⟨boolean-expression ⟩ ) ⟨statement 1⟩ else ⟨statement 2⟩ ---------------------------------- If the boolean-expression true, it excutes statement 1, otherwise statement 2.
if Statement if ( ⟨boolean-expression ⟩ ) ⟨statement⟩ if ( ⟨boolean-expression ⟩ ) { ⟨statements⟩ } if ( ⟨boolean-expression ⟩ ) { ⟨statements⟩ } else { ⟨statements⟩ }
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.
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
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
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⟩
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
do…while do { ⟨statements ⟩}while ( ⟨boolean-expression ⟩ ); • The only difference from while loop is that the statements will be executed >= 1 times.
for Loop for ( ⟨initialization ⟩; ⟨continuation-condition ⟩; ⟨update ⟩ ) { ⟨statements ⟩ } Example -- counting loop for(⟨variable⟩=⟨min⟩; ⟨variable⟩<=⟨max⟩;⟨variable⟩++) { ⟨statements ⟩ }
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.