Instructor: Craig Duckett cduckett@cascadia.edu Introduction to Programming Instructor: Craig Duckett cduckett@cascadia.edu Counters and Counting Loops http://faculty.cascadia.edu/cduckett/demo http://programajama.com/courses/demo
Today’s Topic: Counters and Counting Loops Review: if and while Statements Boolean conditions (true or false) Example code of both statements in action Counters What Good are They? How are They Used? Example code of counter in action
REVIEW: if and while Statements Both the if and while statements evaluate Boolean conditions They test to determine whether a condition is true or false True, do something False, don’t do something Do it! Don’t do it!
CONCEPT: The if Statement IF EXAMPLE: Taking a free right-turn at a red light “If it is clear (no vehicle or pedestrian coming), then take a free right turn.” Do This The Next Thing The Next Thing
The if Statement “If true, do it once …” False
if Example Code WALK-THROUGH: RobotWall.java
CONCEPT: The while Statement (Loop) WHILE EXAMPLE: Buying Gum Balls “While I have enough quarters, buy a gum ball; keep buying gum balls as long as I have enough quarters.” Do This The Next Thing The Next Thing
The while Statement “While true do it again…” False
while Example Code WALK-THROUGH: RobotWall.java
If and While When the simplest form of an if statement asks a question and the answer is true, it executes a group of statements once and then continues with the rest of the program. If the answer to the question is false, that group of statements is not executed. When a while statement asks a question and the answer is true, it executes a group of statements (just like the if statement). However, instead of continuing down to the rest of the program, the while statement asks the question again. If the answer is still true, that same group of statements is executed again. This continues until the answer to the question is false. The if statement’s question is “Should I execute these statements at least once?” if (test condition) { // Statements of things to do } The while statement’s question is “Should I execute these statements again?” while (test condition)
QUESTION With the while loop, what would happen if the condition was always true?
ANSWER An infinite loop!
while Example Code WALK-THROUGH: RobotWall.java
QUESTION Is there a way to configure a set number of times to go through the loop, and then stop or jump out to prevent an infinite loop?
Yes! You can do this by setting up and using a counter! ANSWER Yes! You can do this by setting up and using a counter! I’m a counter. pretty big deal!
What is a Counter ? A counter is a variable, usually of an integer int data type, typically initialized to start with a 0. A variable is a small section of memory that has been set aside based on the size and type of data it will contain. It is given a unique name and then an initial starter value. As the program is run this value can be changed, it can vary, hence the name variable.
Setting Up a Counter In the code, setting up a counter for use with a loop is a 3-Step Process: Create and initialize the counter Create the count condition “Increment” the counter by 1 Let’s take a look at what this means and how it works with our while loop program…
Counter Example Code WALK-THROUGH: RobotWall.java int counter = 0; // (1) Create the counter while(counter < 5) // (2) Create the count condition { lisa.putThing(); lisa.move(); counter = counter + 1; // (3) Increment the counter by 1 } WALK-THROUGH: RobotWall.java
Counter Increment/Decrement int counter = 0; while(counter < 5) { lisa.move(); counter = counter + 1; } int counter = 5; while(counter > 0) { lisa.move(); counter = counter - 1; }
In-Class Exercises From the menu bar, select Lectures and go to the Lectures ## bar and select ICE 5, then ICE 6 to begin working on today's in-class exercises.