Java Expressios & Flow Cotrol Rui Moreira Expressio Separators:. [ ] ( ), ; Dot used as decimal separator or to access attributes ad methods double d = 2.6; Poto poto = ew Poto(2, 3); it i = poto.x; it j = poto.gety(); Array defiitio uses [ ] it[] arrayits = ew it[10]; //Array of primitive it Poto arraypotos[] = ew Poto[8]; //Array of refereces Parethesis chage order of evaluatio of expressios float f=5*(4+8.9); Comma used to separate elemets of a expressio (o a sigle lie) log l1=18l, l2=28l, l3=38l; Semicolo is a termiator (used to termiate expressios) it fact=1; for(it i=2; i<=umber; i++){ fact *= i; Rui Moreira 2 1
Attributio ad Arithmetic: = ++ -- * / % + - (ca be combied) it i=+1, k=15, j=-20, p+=10; i++; k--; ++j; d = 2.6+5*(x-x%2)/10; System.out.prit( Hello + World! ); // + used to cocateate strigs Compariso: ==!= > >= < <= istaceof boolea bool1 = 1==10; // bool1 gets false boolea bool2 = 1!=10; // bool2 gets true boolea bool3 = 1>10; // bool3 gets false boolea bool4 = 1<=10; // bool4 gets true boolea bool5 = poto istaceof Poto; // bool5 gets true Logic: &&! (used oly with boolea) bool6 = bool1 && bool2; // bool6 gets false because bool1 is false (AND) bool6 = bool1 bool2; // bool6 gets true because bool1 is true (OR) bool6 =!bool1; // bool6 gets true, i.e, egatio of bool1 (NOT) if (x<10 && x>5!(y==18)) { // NB: short-circuit operators truth =!rect.isiside(poto); Rui Moreira 3 Bitwise: & ^ ~ (used oly with it or boolea) byte b1 = 63 & 252; // Does bit-by-bit AND 60 10 =0..00111100 2 byte b2 = 63 252; // Does bit-by-bit OR - 255 10 =0..11111111 2 byte b3 = 63 ^ 252; // Does bit-by-bit XOR - 195 10 =0..11000011 2 byte b4 = 7; // 7 10 =00000111 2 byte b5 = ~b4; // Does bit-by-bit COMPLEMENT/NOT -8 10 =11111000 2 See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 63 & 252 (AND example) true if both bits are true 63 10 = 00000000 00000000 00000000 00111111 252 10 = 00000000 00000000 00000000 11111100 ------------------------------------------ 60 10 = 00000000 00000000 00000000 00111100 // 63 ^ 252 (XOR example) - true if both bits are equal 63 10 = 00000000 00000000 00000000 00111111 252 10 = 00000000 00000000 00000000 11111100 ------------------------------------------ 195 10 = 00000000 00000000 00000000 11000011 Rui Moreira 4 2
Bitwise Arithmetic Shift: >> << (left-shift ad right-shift) // oly used with it ad log // with it the shift-value is always betwee 0..31 // with log the shift-value is always betwee 0..63 byte b1 = 128>>1; // b1 gets 64 (divide by 2)- iserts left 0 s b1 = 32<<4; // b1 gets 256 (multiply by 4) b1 = -256>>4; // b1 gets -32 (divide by 4)- copies sigal-bit See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 16 << 5 right-shift example iserts 0 s o the rigth 16 10 = 00000000 00000000 00000000 00010000 512 10 = 00000000 00000000 00000010 00000000 // 16 >> 2 right-shift example sigal bit 0 is preserved 16 10 = 00000000 00000000 00000000 00010000 4 10 = 00000000 00000000 00000000 00000100 // -16 >> 2 right-shift example sigal bit 1 is preserved -16 10 = 11111111 11111111 11111111 11110000-4 10 = 11111111 11111111 11111111 11111100 Rui Moreira 5 Bitwise Logic Shift: >>> (usiged right-shift) // oly used with it ad log it b2 = -12; // this byte is -12 10 =1..00001100 2 b2 = b2>>2; // b2 gets -3 10 =11000011 2 b2 = b2>>>2; // b2 gets +3 10 =0..00000011 2 // The operator >> iserts 0 s i the most sigificat bit See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 16 >>> 2 (usiged right-shift example) sigal bit is same 16 10 = 00000000 00000000 00000000 00010000 4 10 = 00000000 00000000 00000000 00000100 // -16 >> 2 (usiged right-shift example) sigal bit is NOT preserved -16 10 = 11111111 11111111 11111111 11110000 1.073.741.820 10 = 00111111 11111111 11111111 11111100 The last example -16 >>> 2 (-16 >> 2 ) + ( 2 << ~2 ) = 1,073,741,820 Rui Moreira 6 3
Creator: ew <ClassCostructor> Poto p1 = ew Poto(2, 3), p2 = ew Poto(5, 8); Rectagle r1 = ew Rectagle(p1, p2); Cast: (<ClassName>) Object o = ew Poto(1, 4); //o ca referece ay object Poto p2 = (Poto)o; //Casts (forces type coformace) Vector v = ew Vector(); //Vector behaves like growable array v.addelemet(ew Rectagle(p1, p2));//stores ay kid of object Rectagle r = (Rectagle)v.elemetAt(0); //Casts to give type Rui Moreira 7 Cotrol Flow Coditioal executio: q if (i<10) {...; else {...; q switch (i) { case 1:...; default:...; Loops: q for(it i; i<limit; i++){...; q while (i<limit) {...; i++; q do {...; i++; while (i<limit); Special cotrols: q break q Cotiue q label1: while(...) {...; if (...) break label1 q label2: for(...) {...; if (...) cotiue label2 Rui Moreira 8 4
if (<boolea-expressio>) { else { Sytax alteratives: q if (<boolea-expressio>) {...; q if (<boolea-expressio>) {...; else {...; q if (<boolea-expressio>) {...; else if (<boolea-expressio>) {...; q if (<boolea-expressio>) {...; else if (<boolea-expressio>) {...; else {...; Example (NB: short-circuit versus o short-circuit operads): if (i<10 && truth){ //Do ot tests 2 d operad if 1 st fails i++; else if (i<20 & truth){ //Tests all operads i+=20; Rui Moreira 9 switch (<it-expressio>) {... Sytax: switch (<it-expressio>) {// Does ot allow float or log expressios case <it-value>:...; case <it-value>:...; default:...; Example: switch (optio) { // If optio is char/byte/short it is promoted to it case 1: callopefile(); case 2: callsavefile(); default: callexitprogram(); Rui Moreira 10 5
for (<iitialisatio>; <test>; <actualisatio>) {... Sytax: for (<iitialisatio>; <test>; <actualizatio>) { //... Example: for (it i=0, j=20; i<18 && j>5; i++, j++) { // We ca use commas to separate expressios temp += i*j; Rui Moreira 11 while (<boolea-expressio>) {... Sytax: while (<boolea-expressio>) { // This cycle will ot execute if expressio is false // We must iclude code to cotrol stop/break the cycle Examples: while (i<100) { i++; // Sometime it will reach 100 while (keepdoig) { if (temp==value) keepdoig=false; // Stop cycle Rui Moreira 12 6
do {... while (<boolea-expressio>); Sytax: do { // This cycle executes at least oe time e.g., txt-based meu // We must iclude code to stop/break the cycle while (<boolea-expressio>); Examples: do { i++; // Sometime it will reach 100 while (i<100); do { if (temp==value) keepdoig=false; // Stop cycle while (keepdoig); Rui Moreira 13 Exercises Class Poto: q Create a method that moves the poit coordiates by a distace dx ad dy Class Rectagulo: q Create a method that moves the rectagle by a distace dx ad dy q Cotrol the coordiates/positio of the poits give to the Rectagulo costructor. q Create method isiside(poto poit) which checks if the give poit is iside o the Rectagulo Class Date: q Create a class Date for storig date objects (day,moth, year) ad esure that the costructor parameters are valid dates (e.g., moths are betwee 1..12, February as oly 28 or 29 days, etc.) q Provide a method dayselapsed(date d) that returs the umber of days betwee the date object ad date d passed as parameter Class Factorial: q Create a class with several static methods, each calculatig the factorial of a give umber but usig differet cycles provided by Java (cf. for, while ad do). Rui Moreira 14 7