I write from California, with its red tile roofs and bank-breaking housing. We will not be meeting for lecture while I am presenting at the FabLearn 2017 conference, but I have a few videos for you to watch. Just as we saw how
if statements can cause our CPU to jump ahead to different instructions based on some boolean expression, we can also have it jump back to some previous instruction. This action lets us write code that repeats.
These first videos demonstrate two kinds of loops:
for. I encourage you to follow along in Eclipse. Pause the videos and write the code as I write it.
I apologize for the annoying clicking in these. They are caused by a cheap headset that didn’t handle movement well.
The two kinds of loops are technically equivalent—any
for loop can be rewritten as a
while loop, and vice versa. But just as some bikes are meant for roads and others for Lowe’s Creek, each loop lends itself to certain applications. My criteria is this: when the loop logic can be written compactly, I use
for. Otherwise, I use
while. Your book talks about definite vs. indefinite loops. I don’t find this dichotomy well-defined or helpful.
There’s also a
do-while loop that you might want to read about. We won’t discuss it formally. There’s also a
for-each loop that we’ll talk about when we hit arrays.
Your next homework will involve generating images with the
BufferedImage class. It will make heavy use of loops. To help prepare you for processing images, please also watch these videos:
You can follow along with these too—just use your own files. If you are on Windows, your paths will look something like this:
See you next class!