Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Craig Duckett

Similar presentations


Presentation on theme: "Instructor: Craig Duckett"— Presentation transcript:

1 Instructor: Craig Duckett cduckett@cascadia.edu
Introduction to Programming Instructor: Craig Duckett Counters and Counting Loops

2 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

3 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!

4 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

5 The if Statement “If true, do it once …”
False

6 if Example Code WALK-THROUGH: RobotWall.java

7 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

8 The while Statement “While true do it again…”
False

9 while Example Code WALK-THROUGH: RobotWall.java

10 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)

11

12

13 QUESTION With the while loop, what would happen if the condition was always true?

14 ANSWER An infinite loop!

15 while Example Code WALK-THROUGH: RobotWall.java

16 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?

17 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!

18 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.

19 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…

20 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 } WALK-THROUGH: RobotWall.java

21 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; }

22 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.

23


Download ppt "Instructor: Craig Duckett"

Similar presentations


Ads by Google