280 likes | 422 Views
IDS 201 Introduction to Business Programming (Fall 201 4 ). Week3. Increment and Decrement. counter = counter + 1 ; Get the old value of counter from memory (right side); Add 1 to counter; Assign the new value to counter in memory (left side). Incrementing and decrementing operators.
E N D
IDS 201Introduction to Business Programming (Fall 2014) Week3
Increment and Decrement • counter = counter + 1; • Get the old value of counter from memory (right side); • Add 1 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 X++ and ++X Y = X++; (e.g. X = 5;) • The value of Y is the value of expression X++, meaning that Y 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, meaning that Y is the new value of X, after 1 is added. • X = 6; • Y = 6; • For both cases, X will be increased by 1.
Decrement • The difference between X-- and --X Y = X--; (e.g. X = 5;) • The value of Y is the value of expression X--, meaning that Y 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, meaning that Y is the new value of X, after 1 is subtracted. • X = 4; • Y = 4; • For both cases, X will be decreased by 1.
Example int X = 5; int Y = X--; X = 4; Y = 5; int X = 5; int Y = ++X; X = 6; Y = 6;
Conditional Operators • ⟨boolean-expression⟩ ? ⟨expression1⟩ : ⟨expression2⟩ • If the boolean-expression is true, it evaluates expression 1, otherwise expression 2. • E.g., double next= (N % 2 == 0) ? (N/2) : (3*N+1); Equivalently, double next = N/2 if N iseven; double next = 3*N+1 is N isodd;
Type Conversion • The type of an expression not appropriate will leads to errors at a compiling time. • Java is intelligent, different types (related types) can sometimes be converted each other for compatibility. • Explicitly or implicitly. • After conversion, values may be kept or changed.
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, meaning that values may be changed, 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 Character • 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 (top bottom) 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 block. { <statements> } • Most times, we ignore curly brackets. • Empty block { }
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