BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
BIT 115: Introduction To Programming 2 Must partner in class! If your partner is absent, partner with someone else –Groups of 2 to 3 students OK –Sit next to each other! You must take on new partners! –3 person groups may have to become two 2-person groups if they get a new member 2
BIT 115: Introduction To Programming Reading for today: –F.2 - Repeating Statements – while loops – Nesting if/while –F.4 - Temporary Memory (counting loops) –F.5 – Parameters Reading for next class (updated syllabus): –2.2.3 – Instance variables, constructors –6.6 & F.6 - Getting user input, printing output
BIT 115: Introduction To Programming 4 A1 being corrected –Might use the Java Code Critic, check Parts 3&4 were biggest problem –difference between showing errors and tracing –only needed to fix compile-time errors –stop tracing when a crash occurs HOMEWORKHOMEWORK
BIT 115: Introduction To Programming 5 Due next Wed 4/23 –Assignment 2 Should be available this evening –Journal #2 Midterm will probably be Mon 4/28 –Plan to post study notes on the website soon HOMEWORKHOMEWORK
BIT 115: Introduction To Programming 6 while loops Important new syntax: while( ) { // multiple statements here. } You could omit the braces & use just one statement, but don’t. Avoid confusion! 6
BIT 115: Introduction To Programming 7 ICE_05_Demo_1 Review 2nd while loop Run it 7
BIT 115: Introduction To Programming 8 Watch for Infinite Loops! while ( someBooleanExpression() ) { // multiple statements here. } Note that if you don’t do anything to change how the expression evaluates, it’ll always evaluates to the same thing. If the expression always evaluates to true, then you’ll be stuck in an infinite loop!
BIT 115: Introduction To Programming 9 Important points The whole loop can be placed anywhere that a normal command can be placed –i.e., anywhere you could write karel.move(); This includes main (as in ICE_05_Demo_1) 9
BIT 115: Introduction To Programming 10 What can I use for ? ICE_05_Demo_1.java code uses a predicate service –This type of service says that something IS or IS NOT true canPickThing() says either true, the robot is beside a thing (in the same intersection as the thing), or false, the robot is not beside a Thing. predicate \Pred"i*cate\, v. i. To affirm something of another thing; to make an affirmation. --Sir M. Hale. 10
BIT 115: Introduction To Programming 11 ICE 05 ICE Part 1: Trace the code, find errors –USE AN EXTRA COLUMN to show the loop condition ICE Part 2: Write code from a flowchart TELL ME when you are ready for lecture for ICE Part 3! 11
BIT 115: Introduction To Programming 12 Variables, comparing quantities So far, we've only created things which have had on-screen representations –Robot, Thing, Wall, etc. However, we need the program to be able to remember stuff –If we want to compare quantities, we'll have to store numbers –We'll start with just whole numbers: integers 12
BIT 115: Introduction To Programming 13 Variables, comparing quantities 1st, you have to ask Java to get you some memory, and you have to give the memory a name. –If it doesn't have a name, how can you talk about it later? int myNewVariable; Once you've created it, you can put a value into it using the assignment operator =. myNewVariable = 4; 13
BIT 115: Introduction To Programming 14 Variables, comparing quantities When you assign something to the variable, it 'sticks', until you assign something else. int myNewVariable; // create & name it. myNewVariable = 4; // myNewVariable has the value 4 myNewVariable = 10; // myNewVariable has the value 10 myNewVariable = -2; // myNewVariable has the value -2 You can also increase the value stored in the variable numMoves = numMoves + 1; ++numMoves; 14
BIT 115: Introduction To Programming 15 Variables, comparing quantities You need to be able to compare quantities too –Operators you can use: OperatorIs true when: A < B A is less than B A <= B A is less than or equal to B A > B A is greater than B A >= B A is greater than or equal to B A = = B A is equal to B –Notice that this is composed of two equal signs (=) right next to each other A != B A is not equal to B 15
BIT 115: Introduction To Programming 16 Variables, comparing quantities Pulling this all together, we get something like: int numMoves = 0; while (numMoves < 4) { this.move(); numMoves = numMoves + 1; } You should trace this with an extra column for the numMoves values 16
BIT 115: Introduction To Programming 17 Loop Patterns There are certain patterns of code that you'll see again & again –For example, if you want to count from one number to another, you'll end up with code that's similar to every other time you want to do this –These similarities are good to notice Instead or reinventing the wheel each time, REUSE patterns –The details (like what a variable is named, etc.) will change –The important parts, the structure, won't change 17
BIT 115: Introduction To Programming 18 Counting patterns Counting up: page 259 Counting down: page 204 All textbook patterns: page 826 MAKE SURE TO ACTUALLY COUNT UP or DOWN!!!! –If you don't actually increase the value of count- variable by 1, the computer won't do it howManyTimes = howManyTimes + 1; // same as ++howManyTimes; or howManyTimes++; 18
BIT 115: Introduction To Programming 19 ICE Parts 3, 5-6 SKIP Part 4 until Parts 5-6 are done 19