270 likes | 361 Views
GAME102 - Intro IF TRANSFORMATIONS. G. MacKay . What is an “if” transformation?. You know the “if” statement if (test) statement; if (test) statement1; else statement2;. What is an “if” transformation?.
E N D
GAME102 - IntroIF TRANSFORMATIONS G. MacKay
What is an “if” transformation? • You know the “if” statementif (test) statement;if (test) statement1;else statement2;
What is an “if” transformation? • You know the “if” statement with multiple linesif (test){ statement1; statement2;}else{ statement3;statement4;}
What is an “if” transformation? • Transformations allow programmers to simplify codeif (test){ statement1; statement2;}else{ statement3;statement2;} • “statement2” is repeated in both sections
What is an “if” transformation? • SIMPLIFY BYif (test){ statement1;}else{ statement3;}statement2; • “statement2” taken out and placed at the BOTTOM, AFTER the “if”
What is an “if” transformation? • SIMPLIFY BY ELMINATING { }if (test) statement1;else statement3;statement2; • MUCH SIMPLER
Types of “if” transformations? • BOTTOM FACTORING • TOP FACTORING • IMPROPER USE
Top Factoring “if” transformation? • Examine the following codeif (test){ statement1; statement2;}else{ statement1;statement3;} • “statement1” is repeated in both sections
Top Factoring “if” transformation? • Look at the following statement1; if (test){ statement2;}else{ statement3;} • Is this the same as “Bottom Factoring?” • NO!!!!!
Top Factoring “if” transformation? • statement1; if (test){ statement2;}else{ statement3;} • This ONLY works under the following condition“statement1” MUST NOT affect the “if” test
Top Factoring “if” transformation? • Examine the following codeif (x>20){ x=10; y=y+z+2*x;}else{ x=10;y=2*z-y;} • “x=10” is repeated in both sections
Top Factoring “if” transformation? • Examine the following codeif (x>20){ x=10; y=y+z+2*x;}else{ x=10;y=2*z-y;} • “x=10” is repeated in both sections
Top Factoring “if” transformation? • x=10; if (x>20){ y=y+z+2*x;}else{ y=2*z-y; } • DOES NOT WORK THE SAMEChecking for “x>20” and modifying “x=10”!!!!
Top Factoring “if” transformation? • Examine the following codeif (y>20){ x=10; y=y+z+2*x;}else{ x=10;y=2*z-y;} • “x=10” is repeated in both sections
Top Factoring “if” transformation? • x=10; if (y>20){ y=y+z+2*x;}else{ y=2*z-y; } • WORKS THE SAME AND IS PERMITTED!Checking for “y>20” and modifying “x=10”!!!!
Improper Use “if” transformation? • Examine the following codeif (y>20+x) ;else { z=10*x*y; y=y+z+2*x; x=20-y;} • There is nothing to do in the “THEN” section of the “if” statement (IMPROPER)
Improper Use “if” transformation? • Look at the following codeif (y<=20+x) { z=10*x*y; y=y+z+2*x; x=20-y;} • Reverse the test!“>” becomes “<=“
The BEAUTY of “if” transformations? • You do NOT have to understand the code! • Follow RULES of transformation and simplify code
Who uses “if” transformations? • Good software design should NEVER produce code that requires “if” transformations. • Software maintenance commonly requires the use of “if” transformations
Who uses “if” transformations? • if (light_bulb1_on){x_power = 40;}else{x_power= 40;}
Who uses “if” transformations? • BOTTOM FACTORif (light_bulb1_on){}else{}x_power= 40;
Who uses “if” transformations? • SIMPLIFIES TOx_power= 40; • Oh… BTW…this is based on a REAL case found while working in California
Dangling “else”? • Look atif (x>20)if (y>20) y=y+z+2*x;else{ x=10; y=2*z-y;}
Dangling “else”? • DOES the “else” go with the 1st or 2nd test?if (x>20)if (y>20) y=y+z+2*x;else{ x=10; y=2*z-y;}
Dangling “else”? • Remember the indentation does not matter to compiler!!!!if (x>20)if (y>20) y=y+z+2*x;else{ x=10; y=2*z-y;}
Dangling “else”? • RULE – goes with the last non-terminated “if”if (x>20)if (y>20) y=y+z+2*x;else{ x=10; y=2*z-y;}
Dangling “else”? • Use { } to clarifyif (x>20){if (y>20) y=y+z+2*x; else {x=10; y=2*z-y; }}