1 / 27

GAME102 - Intro IF TRANSFORMATIONS

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?.

bree
Download Presentation

GAME102 - Intro IF TRANSFORMATIONS

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. GAME102 - IntroIF TRANSFORMATIONS G. MacKay

  2. What is an “if” transformation? • You know the “if” statementif (test) statement;if (test) statement1;else statement2;

  3. What is an “if” transformation? • You know the “if” statement with multiple linesif (test){ statement1; statement2;}else{ statement3;statement4;}

  4. What is an “if” transformation? • Transformations allow programmers to simplify codeif (test){ statement1; statement2;}else{ statement3;statement2;} • “statement2” is repeated in both sections

  5. What is an “if” transformation? • SIMPLIFY BYif (test){ statement1;}else{ statement3;}statement2; • “statement2” taken out and placed at the BOTTOM, AFTER the “if”

  6. What is an “if” transformation? • SIMPLIFY BY ELMINATING { }if (test) statement1;else statement3;statement2; • MUCH SIMPLER

  7. Types of “if” transformations? • BOTTOM FACTORING • TOP FACTORING • IMPROPER USE

  8. Top Factoring “if” transformation? • Examine the following codeif (test){ statement1; statement2;}else{ statement1;statement3;} • “statement1” is repeated in both sections

  9. Top Factoring “if” transformation? • Look at the following statement1; if (test){ statement2;}else{ statement3;} • Is this the same as “Bottom Factoring?” • NO!!!!!

  10. Top Factoring “if” transformation? • statement1; if (test){ statement2;}else{ statement3;} • This ONLY works under the following condition“statement1” MUST NOT affect the “if” test

  11. 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

  12. 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

  13. 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”!!!!

  14. 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

  15. 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”!!!!

  16. 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)

  17. 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 “<=“

  18. The BEAUTY of “if” transformations? • You do NOT have to understand the code! • Follow RULES of transformation and simplify code

  19. Who uses “if” transformations? • Good software design should NEVER produce code that requires “if” transformations. • Software maintenance commonly requires the use of “if” transformations

  20. Who uses “if” transformations? • if (light_bulb1_on){x_power = 40;}else{x_power= 40;}

  21. Who uses “if” transformations? • BOTTOM FACTORif (light_bulb1_on){}else{}x_power= 40;

  22. Who uses “if” transformations? • SIMPLIFIES TOx_power= 40; • Oh… BTW…this is based on a REAL case found while working in California

  23. Dangling “else”? • Look atif (x>20)if (y>20) y=y+z+2*x;else{ x=10; y=2*z-y;}

  24. 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;}

  25. 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;}

  26. 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;}

  27. Dangling “else”? • Use { } to clarifyif (x>20){if (y>20) y=y+z+2*x; else {x=10; y=2*z-y; }}

More Related