COMP 110 Loops, loops, loops, loops, loops, loops…

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Lesson #5 Repetition and Loops.
REPETITION CONTROL STRUCTURE
CS 1430: Programming in C++ No time to cover HiC.
Lesson #5 Repetition and Loops.
Loop Structures.
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Counter control Loop
While Loops in Python.
Topics Introduction to Repetition Structures
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Logical Operators and While Loops
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Control Structure Senior Lecturer
While Loops.
The while Looping Structure
Lesson #5 Repetition and Loops.
Chapter 4 Loops While loop The for loop do… while break and continue
The while Looping Structure
Module 4 Loops.
Chapter 6: Repetition Statements
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Let’s all Repeat Together
Using the Target Variable Inside the Loop
CprE 185: Intro to Problem Solving (using C)
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Flowcharts and Pseudo Code
A LESSON IN LOOPING What is a loop?
Logical Operators and While Loops
CS150 Introduction to Computer Science 1
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Java LESSON 3 Loops.
Loops.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
The while Looping Structure
The while Looping Structure
CprE 185: Intro to Problem Solving (using C)
Michele Weigle - COMP 14 - Spr 04
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

COMP 110 Loops, loops, loops, loops, loops, loops… Michele Weigle - COMP 14 - Spr 04 COMP 110 Loops, loops, loops, loops, loops, loops… Luv Kohli September 22, 2008 MWF 2-2:50 pm Sitterson 014

Michele Weigle - COMP 14 - Spr 04 Announcements Program 2 due date extended to Friday, Sep 26, 2pm Lab 4 due date extended to Wednesday, Oct 1, 2pm Print out your programs and hand the in Interesting talk today at 4pm, SN011 William Swartout, “Toward the Holodeck: Integrating Graphics, Artificial Intelligence, Entertainment and Learning”

Michele Weigle - COMP 14 - Spr 04 Questions? How is Program 2 going? Any other questions?

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 More loop examples If time: indentation

Designing a loop Initializing statements Loop body Ending a loop

Designing a loop body Example: calculate the sum of numbers entered by the user

Identify the loop body from pseudocode Output instructions to the user Initialize variables Prompt user for input Read a number into variable next sum = sum + next; ... Output the sum Repeated statements become your loop body Statements that are only done once are not part of your loop body

Pseudocode with loop body Output instructions to the user Initialize variables Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum Initializing statements How do we end the loop? How many iterations?

Initializing statements Variables used in your loop need to be initialized (set to a value) before the loop next Read a number into variable next We read a new value for next before using it during each iteration of the loop so we do not need to initialize it sum sum = sum + next; sum is on the right side of an assignment statement. sum MUST have a valid value before the loop starts.

What should sum’s initial value be? Output instructions to the user Initialize variables Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum

Initialize sum Consider the first iteration. After executing the first iteration, the expected value of sum should be sum == next (the first input value) The assignment statement is sum = sum + next; Therefore, initial value of sum is sum = 0;

What should sum’s initial value be? Output instructions to the user sum = 0; Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum Initializing statements

Initializing statements Michele Weigle - COMP 14 - Spr 04 Initializing statements Always set initial value to 0? What if we calculate the product of all the input values? product = product * next; After the first iteration, we expect product == next (the first input value) Initial value of product? product = 1;

Ending a loop How do we end the loop? How many iterations? Output instructions to the user sum = 0; Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output the sum How do we end the loop? How many iterations?

Ending a loop Count-controlled loops User-controlled loops If you know the number of loop iterations for (count = 0; count < iterations; count++) User-controlled loops Ask-before-iterating Sentinel value

Count-controlled loops You know how many iterations in advance. Ask every student in the class for his/her age and calculate the average age sum = 0; for (student 1 to student n) { Ask his/her age sum = sum + age; } average = sum / n;

Count-controlled loops Use a for loop in most cases 37 students in the class: 37 iterations numStudents = 37; sum = 0; for (count = 1; count <= numStudents; count++) { Ask student #count for his/her age sum = sum + age; } average = sum / numStudents;

Count-controlled loops Can also use a while loop, but for loop is easiest way to implement count-controlled loops numStudents = 37; count = 1; sum = 0; while (count <= numStudents) { Ask student #count for his/her age sum = sum + age; count = count + 1; } average = sum / numStudents;

User-controlled loops Also called ask-before-iterating Ask the user if it is time to end the loop Example: add a bunch of numbers Lab 4: when the user enters a negative number (a sentinel value), it’s time to end the loop

User-controlled loops Use while and do-while in most cases do-while If you know that the user wants at least one iteration Ask the user whether the loop should end at the end of the loop body

User-controlled loops: do-while do { // do stuff in your code here Prompt user for input Read a number into variable next sum = sum + next; // ask if we should do another iteration System.out.print(“Continue (yes/no)? ”); answer = keyboard.nextLine(); } while (answer.equalsIgnoreCase(“yes”));

User-controlled loops: while Use while if there could be zero iterations Example, summing numbers: When the user inputs a negative value: End the loop The negative value is not used in calculating the sum If the first input value is negative, no iterations

User-controlled loops: while sum = 0; while (next >= 0) { Prompt user for input Read a number into variable next sum = sum + next; } Is this code correct? What is the initial value of next?

Solution sum = 0; Prompt user for input Read a number into variable next while (next >= 0) { sum = sum + next; } Is this code correct?

Solution, another try sum = 0; Prompt user for input Read a number into variable next while (next >= 0) { sum = sum + next; } Is this code correct?

Example problem Find the lowest and highest ages in the class Use a loop? Yes What goes in the loop body?

Loop body Ask student 1 for age Update min/max ages Ask student 2 for age ... Ask student 37 for age End loop Output min and max ages

Min/max ages Which kind of loop? Let’s use a for loop

Min/max ages int min = 2000; // initialize to large value int max = 0; // initialize to small value for (int count = 1; count <= 37; count++) { Ask student #count for age if (age > max) max = age; if (age < min) min = age; } Output min and max ages

Min/max ages Ages max min 20 23 18 25 12 94 36 … 20 23 25 94 2000 20 20 23 25 94 2000 20 18 12 if (age > max) max = age; if (age < min) min = age;

Michele Weigle - COMP 14 - Spr 04 Wednesday Nested loops Loop bugs