Instructor: Craig Duckett

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

Repeating Actions While and For Loops
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Engineering 1020 Introduction to Programming Peter King Winter 2010.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
National Diploma Unit 4 Introduction to Software Development Data Structures – Loops and selection.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Sensor Information: while loops and Boolean Logic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CE En 270 Brigham Young University Norm Jones
Follow up from lab See Magic8Ball.java Issues that you ran into.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
FOP: While Loops.
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
BIT115: Introduction to Programming
Introduction To Repetition The for loop
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
ECS10 10/10
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Topic 5 for Loops -Arthur Schopenhauer
Programming Fundamentals
Web Programming– UFCFB Lecture 16
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
Introduction to Programming
Lecture Notes – Week 3 Lecture-2
Introduction to Object-Oriented Programming with Java--Wu
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Module 4 Loops.
Three Special Structures – Case, Do While, and Do Until
ICT Programming Lesson 3:
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
FLUENCY WITH INFORMATION TECNOLOGY
BIT116: Scripting Lecture 6 Part 1
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Learning Plan 4 Looping.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
This shows running the first guess number program which only allows one guess - I show the correct answer for testing purposes.
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

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.