Counting Loops.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Looping Structures: Do Loops
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Chapter 6 - Visual Basic Schneider
Copyright © Texas Education Agency, Computer Programming For Loops.
5.05 Apply Looping Structures
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Introduction to Computer Programming Counting Loops.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Tutorial 6 The Repetition Structure
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Visual Basic Programming
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Chapter 7 Problem Solving with Loops
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Chapter 9 Repetition.
UNIT 5 Lesson 15 Looping.
REPETITION CONTROL STRUCTURE
while Repetition Structure
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Introduction To Repetition The for loop
Chapter 5: Loops and Files.
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 5 Repetition.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 9 Control Structures.
Chapter (3) - Looping Questions.
Chapter 8: More on the Repetition Structure
Manipulating Pictures, Arrays, and Loops
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Counting Loops

Introduction to Loops: The Counting Loop Chap 2 section 2.3 A loop is a repeating structure that contains a block of program statements. Needs a way to indicate which statements repeat Needs a way to start

Repeat Something The a parameter sets a number on and the program needs to count up to that number by displaying each number on the console until it reaches that number Assume the parm is going to hold 5. The program should show: 1 2 3 4 5 Code that now.

Repetition Code int intNumber = 1; System.out.println(intNumber); intNumber = intNumber + 1;

Repeat More Often Change count to 10 times. Annoying??? See the repetition? Loop the same commands over and over again in a pattern

Loop Syntax Tell the computer to loop To do it 5 times: Setup: Create an integer to count with Start where? Start at 1 Increment? Keep adding 1 your integer Stop where? Stop at 5 Watch your counter by printing it During After

Your Code with a Loop int intNumber ; for (intNumber = 1; intNumber <= 5; intNumber= intNumber+1) { System.out.println(“Inside loop “ + intNumber); } System.out.println(“After loop: “ + intNumber);

Flowchart of For…Next Loop intCount <= 10? Display "Hello" Yes No Set intCount to 1 Add 1 to intCount

You try it - Counting Count from 5 to 50 by 5’s Setup: Create an integer to count with Start where? Start at ___ Increment? Keep adding ___ your integer Stop where? Stop at ___

Your Code with a Loop start test increase int intNumber ; for (intNumber = 5; intNumber <= 50; intNumber= intNumber+5) { System.out.println(“Inside loop “ + intNumber); } System.out.println(“After loop: “ + intNumber);

Add Up Need a bucket to hold the total Start at 0 Keep accumulating each pass Make your numbers sum up

Logic for Keeping a Running Total Read the next number Is there another number to read? Set accumulator to 0 Add the number to the accumulator Yes (True) No (False) Setting the accumulator variable to zero before entering the loop is a critical step

Code that Totals int intSum = 0; int intNumber ; for (intNumber = 1; intNumber= intNumber+1) { intSum = intSum + intNumber; System.out.println(“Inside loop “ + intNumber ); System.out.println(“running count: “ + intSum ); } System.out.println(“After loop total: “ + intSum);

Accumulating a total Variables don't remember what they were You can create a variable to increase and run it each time the loop runs. double total = 0; for (int count = 1; count <= 3; count++) { total = total + count;} // add what you are counting to what you have System.out.println(total); at end of first loop, count = 1 and total = 1 +0 = 1 at end of second loop, count = 2 and total = 2 + 1 = 3 at end of third loop, count = 3 and total = 3 + 3 = 6 at end of entire loop, count is gone and total = 3

You try – totalling What is the total of the series from 5 to 50 by 5’s? Create a variable to hold the total Set that variable to 0 – before you start repeating Add to the variable repeatedly (inside loop) After the loop – you have your total.

Your Code Totalling with a Loop int intNumber ; int intSum= 0; for (intNumber = 5; intNumber <= 50; intNumber= intNumber+5) { intSum = intSum + intNumber; System.out.println(“Inside loop “ + intNumber ); System.out.println(“running count: “ + intSum ); } System.out.println(“After loop total: “ + intSum);

Summary of Loops Repetition Starts with For () { Ends with : matching } Starting value: 1rst statement inside () Go Condition – 2nd statement inside () Increment – last statement inside () Total – Create bucket starting at zero Accumulate inside loop Have total after loop Next: Loops inside loops!