BIT 115 Lecture 4 Page 1 / 3 NOTE TO STUDENTS: These notes are for the instructor's use. If you happen to find them to be useful, that's great. If not, then please ignore these. Thanks! —The Instructor Lecture 4: Intro Slide: <Leave up while class accumulates> << quiz >> Conditional (If) statements What if we want the robot to pick up a Thing, but we don't know if it's in the intersection with the robot or not? There might be none. pickThing robot breaks There might be one thing pickThing works fine For what we've seen so far, we can actually figure out what the program will do at compile time However, this might not always be possible video games use randomization to provide a different experience each time The user may give us different input each time. We need a way of having our program do something different, depending on exactly what's already happened. Further, this pattern occurs very frequently – so frequently that Java has a concise way to express it: if(this.canPickThing()) { this.pickThing(); } this.nextCommand(); It reads kinda like in English – if this robot is beside a thing, then pick it up, then go to the next command If this robot isn't beside a thing, you simply go directly to the next command AFTER the closing curly brace. Tracing the code: Write down the line with the IF on it, and note whether it's true or not if it is true Write down the first line inside the If statement (this.pickThing()) Then use the next line to write down the robot's state, like normal if it's not true Write down the line AFTER the curly brace Then use the next line to write down the robot's state, like normal Syntax Points: BIT 115 LECTURE 4 Page 1 / 3 BIT 115 Lecture 4 Page 2 / 3 1. if is case-sensitive 2. You NEED to have the ( and ) 3. You (kinda) NEED to have the { and } (well, actually you don't, but for now just pretend like you do – it'll make your life easier) 4. You can put white-space pretty much wherever you want. 5. Anything you can use for a while's test, can be used in an if's test Comparison: You can use <, >, <=, >=, or == (equals), or != (not equals) to compare numbers There are various services that you can use to ask the robot questions. such as: how many things do you have in your backpack? joe.countThingsInBackpack() We can combine the two, and ask a question about the robot if(joe.countThingsInBackpack() < 3) { // there are strictly less than 3 Things // in Joe's backpack ... } ICE: Find compiler + intent errors. <Go over the flowchart bit> ICE: Some code using an if statement. If-Else if( this.frontIsClear() ) { this.move(); } else { System.out.println("Hey, I almost crashed into a wall!"); } this.pickThing(); Important Points: 1. else is lowercase 2. else must follow directly after the close curly-brace for the if part 3. The rules for open/close curly braces after the else follow the same rules as for the if statement 4. Just like a normal "if" statement, you do one or the other, JUST ONCE 5. How to trace: Ask the question after the if ("Is the front clear?") a. If the answer is yes (i.e., TRUE), then do everything between if & else, then skip the else clause (in this case, jump to the pickThing) BIT 115 LECTURE 4 Page 2 / 3 BIT 115 Lecture 4 Page 3 / 3 b. If the answer is no (i.e., FALSE), then skip everything between if & else, then do everything between the else & it's closing curly brace. THEN, keep going (in this case, pickThing) This means that you MUST do either the if, or else part – one (but not both) of them! <Walk students through the solution idea for the 'which hallway' problem> ICE: Figure out which hallway to go down BIT 115 LECTURE 4 Page 3 / 3