Counted Loops.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Computer Science 1620 Programming & Problem Solving.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
CPS120 Introduction to Computer Science Iteration (Looping)
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Introduction to Computers and Programming Lecture 7:
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Information and Computer Sciences University of Hawaii, Manoa
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Java Course Review.
Repetition Structures Chapter 9
Repetition-Counter control Loop
Ch 7: JavaScript Control Statements I.
Lecture 07 More Repetition Richard Gesick.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Lecture 4B More Repetition Richard Gesick
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Arrays, For loop While loop Do while loop
Control Structure Senior Lecturer
Unit 2 Programming.
An Introduction to Java – Part I, language basics
Additional Control Structures
In this class, we will cover:
Programming Funamental slides
Java Programming Arrays
Java Programming Loops
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Module 4 Loops.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
1D Arrays and Lots of Brackets
The structure of programming
In this class, we will cover:
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Repetition Statements
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Loops CGS3416 Spring 2019 Lecture 7.
Arrays Introduction to Arrays Reading for this Lecture:
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
loops revisited I/O arrays
Presentation transcript:

Counted Loops

What is a Loop? In programming, a loop is a sequence of instructions that is continually repeated until some kind of end condition is met. We call one full cycle of a loop an iteration “How many times does that loop iterate?” “What iteration did the error occur during?” Useful Applications of Loops: A password program that limits attempts before denying access Analyze a String, character by character for encryption (loop based on the String length) Repeating a process, such as drawing 10 of one thing on the screen Game Loops repeat the same two actions 60 times per second, Update & Draw Animation continually loops through all the images in a single animation over a period of time Working with arrays, we can scan through each element one at a time

Applied Usefulness Add the numbers from 1 to 1000 and output the sum sum = sum + 1; Code Description: sum = sum + 2; FOR each number from 1 to 1000 sum = sum + 3; COMPUTE sum as sum plus number ... ENDFOR sum = sum + 1000; console.log(“sum: “ + sum);

For loops A counted loop is a loop that iterates a specific number of times. In most languages, a counted loop is also called a for loop because it uses the keyword, for For loops are made up of 4 parts: (3 to define the for loop, 1 to be executed) Define a counter variable (used to count iterations) Compare counter against an end value to continue looping Change counter in a manner that gets it closer to the end value Code to execute each iteration of the loop

For loops - 3 Parts in the for statement 1 ) Define a counter variable int count = startValue This variable can be defined before the for loop if you want to keep the data after the loop completes. It can also be defined when defining the for loop itself. However, this strategy will delete the data when the loop completes. startValue can be any integer or even another variable The name of the counter variable (count in the example) can be simple to save time. The most common count variable names in for loops are i and j. As shown here, the counter variable should be an int type

For loops - 3 Parts in the for statement 2) Compare the counter against an endValue. count < 10 or count >= 0 endValue must also be an int type If your loop is counting up, endValue must be greater than startValue Otherwise if your loop is counting down, endValue must be less than startValue The Loop continues to repeat as long as the test evaluates to true This test occurs before ever entering the loop code, which means the code may never execute

For loops - 3 Parts in the for statement 3) Change the counter count = count + 1 or count = count - 1 The amount to increase/decrease the loop counter by at the end of iteration This is also referred to as incrementing the counter You can change the counter in any way you wish as long as it is progressing towards the endValue E.g. count = count + 5 or count = count / 2 or count = Math.Pow(count,2) Failure to progress towards endValue will result in an endless(infinite) loop Remember your shortcuts: count = count + 1  count += 1  count++ count = count - 1  count -= 1  count--

For loops – Example Counting Up Create a for loop that counts from 1 to 1000, and keeps a running total of the count int total = 0; for (int count = 1; count <= 1000; count = count + 1) { total = total + count; } 1) Define 2) Compare 3) Change 4) Code

Example Program for (int i = 10; i > 0; i = i – 1) { System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:10 Iteration number:9 Iteration number:8 Iteration number:7 Iteration number:6 Iteration number:5 Iteration number:4 Iteration number:3 Iteration number:2 Iteration number:1

Example Program 2 for (int i = 10; i > 0; i -= 2) { System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:10 Iteration number:8 Iteration number:6 Iteration number:4 Iteration number:2

Example Program 3 for (int i = 3; i <= 12; i += 3) { System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:3 Iteration number:6 Iteration number:9 Iteration number:12

Arrays and Counted Loops Assume you have an array of 10 numbers. How would you write a subprogram that iterated through the array one element at a time and calculate the average number? private static void Main(String [] args) { double numbers = new double[]{9, 3.2, 4, 7, 6, 6, 3, 9, 10, 1}; double average = FindAverage(numbers); System.out.println(“Average number: “ + average); } private double FindAverage(double[] nums) int sum = 0; int avg = 0; for (int i = 0; i < nums.length; i++) sum = sum + nums[i]; avg = sum / nums.length; return avg; Pass the array as a parameter Q: What is nums.length? A: 10  for (int i = 0; i < 10; i++) Meaning, through the life of the for loop, i will have the values 0 through 9…all the possible index values in our Array!! This allows us to access each sequential element of the array by using the counter variable as our index…AMAZING!

Nesting As mentioned in the Selection lessons, any control structure inside of another is called nesting. if inside an if switch inside an if (or vise versa) loop inside a loop loop inside an if (or vise versa) You Can nest as many loops as you want, but it can get confusing with more than 3 loops inside of loops

Nesting Example What output would this produce? Starting Row: 1 Starting Column: 1 for Row: 1 Starting Column: 2 for Row: 1 Starting Column: 3 for Row: 1 Ending Row 1 Starting Row: 2 Starting Column: 1 for Row: 2 Starting Column: 2 for Row: 2 Starting Column: 3 for Row: 2 Ending Row 2 Starting Row: 3 Starting Column: 1 for Row: 3 Starting Column: 2 for Row: 3 Starting Column: 3 for Row: 3 Ending Row 3 Nesting Example for (int row = 1; row < 4; row++) { System.out.println(“Starting Row: “ + row); for (int column = 1; column < 4; column++) System.out.println(“Starting Column: “ + column + “ for Row: “ + row); } System.out.println(“Ending Row: “ + row); What output would this produce? Think of it like a spreadsheet, you look at one row at a time and work your way from left to right looking at each column. When the row is done you go to the next row and repeat the process until there are no more rows remaining.

Nesting Example What output would this produce? Starting Row: 1 Starting Column: A for Row: 1 Starting Column: B for Row: 1 Starting Column: C for Row: 1 Ending Row 1 Starting Row: 2 Starting Column: A for Row: 2 Starting Column: B for Row: 2 Starting Column: C for Row: 2 Ending Row 2 Starting Row: 3 Starting Column: A for Row: 3 Starting Column: B for Row: 3 Starting Column: C for Row: 3 Ending Row 3 Nesting Example for (int row = 1; row < 4; row++) { System.out.println(“Starting Row: “ & row); for (int column = 1; column < 4; column++) if (column == 1) System.out.println(“Starting Column: “ + “A” + “ for Row: “ & row); } else if (column == 2) System.out.println(“Starting Column: “ + “B” + “ for Row: “ & row); else if (column == 3) System.out.println(“Starting Column: “ + “C” + “ for Row: “ + row); System.out.println(“Ending Row: “ + row); What output would this produce?

Nesting Example Conditionals Inside Loops for (int number = -3; number < 33; number++) { //if the remainder of dividing number by 5 is 0, then it is evenly divisible if (number % 5 == 0 ) System.out.println(number + “ is evenly divisible by 5”); } 0 is evenly divisible by 5 5 is evenly divisible by 5 10 is evenly divisible by 5 15 is evenly divisible by 5 20 is evenly divisible by 5 25 is evenly divisible by 5 30 is evenly divisible by 5