Lecture 5 Notes

advertisement
BIT 115
Lecture 5
Page 1 / 4
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 5
Intro Slide:
<Leave up while class accumulates>
<< quiz >>
While Loops
Go through the first demo on the overhead
Important new syntax:
while( <expression> )
{
// multiple statements here.
}
You could omit the braces & use just one statement, but I’d advise against it.
Flow control:
1. When the program arrives at the while line, evaluate <expression>.
2. If it evaluates to true, then it does the statement following. I’d recommend using the block statement
– { /* multiple statements here */ }.
3. Then, once you reach the bottom brace, you go back to the while statement. (and go back to step 1,
above)
Note that if you don’t do anything to change how <expression> evaluates, it’ll always eval to the same
thing. If <expression> always evaluates to true, then you’ll be stuck in an infinite loop.
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
What can I use for <expression>?
 The demo code uses a predicate service
This type of service says that something IS or IS NOT true
e.g., besideThing() 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.
Source: Webster's Revised Unabridged Dictionary, © 1996, 1998 MICRA, Inc.
BIT 115
SUMMER 2012
Page 1 / 4
BIT 115

Lecture 5
Page 2 / 4
You can compare two quantities
We'll look at this in a little bit.
Also, trace the code enough to give them the idea of how to do it.
ICE: Trace the code, find errors
ICE: Write code from a flowchart
Variables, comparing quantities
You've all seen this some, last lecture.
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
First, 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;
When you assign something to the variable, it 'sticks', until you assign something else.
int myNewVariable; //
myNewVariable = 4;
// myNewVariable
myNewVariable = 10;
// myNewVariable
myNewVariable = -2;
// myNewVariable
create & name it.
has the value 4
has the value 10
has the value 10
You can also increase the value stored in the variable by one, like so:
numMoves = numMoves + 1;
This is great, but you need to be able to compare stuff, too.
Operators you can use:
Operator
Is true when:
A < B
A is less than B
A <= B
A is less than or equal to B
BIT 115
SUMMER 2012
Page 2 / 4
BIT 115
Lecture 5
A > B
A >= B
A == B
Page 3 / 4
A is greater than B
A is greater than or equal to B
A is equal to B
Notice that this is compose of two equal signs (=)
right next to each other
A is not equal to B
A != B
pulling this all together, we get something like:
int numMoves = 0;
while (numMoves < 4)
{
this.move();
numMoves = numMoves + 1;
}
(trace this, with the extra column for numMoves)
ICE: Basic counting
Loop Patterns: Counting
There are certain arrangements 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 stuff
I'm going to call these 'patterns'
The details (like what a variable is named, etc) will change
The important parts, the structure, won't.
Looping until something is true
Looping until something is not true
Pattern: Counting
Summary: Counting up
int howManyTimes;
howManyTimes = 0; // programmers like to start counting at 0 :)
while(howManyTimes < 4) // this loop will be run a total of 4
times
{
joe.putThing();
joe.move();
howManyTimes = howManyTimes + 1;
}
Important Points:
// Step 1
// We'll need to keep track of how many times we've done <whatever>
// so create some space to store that
int howManyTimes;
BIT 115
SUMMER 2012
Page 3 / 4
BIT 115
Lecture 5
Page 4 / 4
// Step 2
// We'll need to keep start counting at some number, which we'll set
// here.
// Giving the variable an initial value is usually called initialization.
// As in "I initialized howManyTimes to 0"
howManyTimes = 0; // programmers like to start counting at 0 :)
// Step 3
// At this point, we actually go through the loop
//
// If we're counting up, we need to say that 'while the counter is less
// than our target number'. If we're counting DOWN, we need to say
// 'while the counter is greater than our target number'
//
// We need to make sure that the target number is correct // Tracing this (either with the trace table, or mentally, once you get
// more comfortable with it) will verify that you've got the right
// target number
while(howManyTimes < 4) // this loop will be run a total of 4 times
{
// Step 4: Do the work
// This is, after all, the whole point of doing this :)
//Step 5
// MAKE SURE TO ACTUALLY COUNT!!!!
// If you don't actually say "increase the value of count by 1",
// the computer won't do it.
howManyTimes = howManyTimes + 1; // same as "howManyTimes++;"
}
Notice that in this example we counted up
We can also count down, or count from any given number to any other number
(E.g., we can move from the avenue that we're on up to, say, Avenue 12)
ICE: Count up, or down, or from one number to another.
BIT 115
SUMMER 2012
Page 4 / 4
Download