Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }

Similar presentations


Presentation on theme: "1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }"— Presentation transcript:

1

2 1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }

3 2 karel_part3_ifElse Indenting by example How to properly indent while writing if/else structures by example! public void someMethod() { if ( ) { codeGoesHere(); allAdditionalCodeLinesUp(); soAllFirstLettersAreInTheSameColumn(); } else { sameSpacingAfterTheElse(); asWhatWasUsedAfterTheIf(); } untilAnotherIfStatementIsEncountered(); }

4 3 karel_part3_ifElse A word about indenting How to properly indent while writing if structures! Every time an if is used, each line after the curly bracket, {should be spaced over to the right a constant amount (say 3 spaces). –To simplify, the first letter of each method/line-of-code should be under the ( in the if statement. The same spacing is used between the brackets that follow the else When the if/else is terminated ( closing curly bracket } ), the following lines of code are moved left by the same number of spaces used above (3 was used above). –To simplify, the first letter of each method/line-of-code should be under the i in the if statement. Please review previous slide

5 4 karel_part3_ifElse A Final Indenting example if ( frontIsClear()) { move(); turnLeft(); } else { turnLeft(); if ( frontIsClear()) { move(); } turnLeft();

6 5 karel_part3_ifElse Sample Code with Objects if (karel.frontIsClear() ) { karel.move();// no danger of hitting wall } else { karel.turnLeft(); }

7 6 karel_part3_ifElse Sample Method public void moveOrTurnLeft() { if ( frontIsClear() ) { move(); // no danger of hitting wall } else { turnLeft(); }

8 7 karel_part3_ifElse Simplify – bottom factoring if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } if ( facingSouth() ) { turnLeft(); } else { turnRight(); } move(); move();move();move();

9 8 karel_part3_ifElse Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); }

10 9 karel_part3_ifElse Simplify – top factoring move(); // changes value of boolean expression if ( beeperOnLeft() ) { turnLeft(); } else { turnRight(); }

11 10 karel_part3_ifElse being redundant again and again and again ha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } if ( facingNorth() ) { move(); pickTwoBeepers(); turnLeft(); }

12 11 karel_part3_ifElse Your Assignment Implementing the followAllBreadCrumbs method declared in HanselAndGretelRobot class. The followAllBreadCrumbs method will invoke the followBreadCrumb method 56 times. No method may contain more than seven commands. You will need to implement the followBreadCrumb method as described in handout ( Karel_part2_ifElse.doc ) for details.


Download ppt "1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }"

Similar presentations


Ads by Google