Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java Programming: From the Ground Up
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
CS101 Computer Programming I Chapter 4 Extra Examples.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Chapter 4: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Repetition Statements
Chapter 4 Repetition Statements (loops)
Loops.
Chapter 5: Control Structures II
Loop Structures.
Java's for Statement.
Computer Programming Methodology Input and While Loop
Repetition-Counter control Loop
CSS 161: Fundamentals of Computing
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.
Flow Control in Java.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Iteration with While You can say that again.
CSS161: Fundamentals of Computing
Michele Weigle - COMP 14 - Spr 04
Outline Altering flow of control Boolean expressions
COMP 110 Loops, loops, loops, loops, loops, loops…
Module 4 Loops.
Lec 4: while loop and do-while loop
Loop Strategies Repetition Playbook.
Logical Operators and While Loops
Repetition Statements (Loops) - 2
Repetition Statements
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 3 Debugging Section 3.4
CSS161: Fundamentals of Computing
Chapter 3 Flow of Control Loops in Java.
Michele Weigle - COMP 14 - Spr 04
Presentation transcript:

Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011 COMP 110 Loops Catie Welsh February 14, 2011 1

Michele Weigle - COMP 14 - Spr 04 Announcements Program 2 Due Wednesday by 11:59pm 2

Michele Weigle - COMP 14 - Spr 04 Questions? 3

Michele Weigle - COMP 14 - Spr 04 Today in COMP 110 Loops Body Initializing Statements Ending a loop Bugs 4

Michele Weigle - COMP 14 - Spr 04 Loop Body count = 1; while (count <= num) { System.out.print(count + “, “); count++; } Repeated code Write pseudocode and turn repeated statements into loops 5

Michele Weigle - COMP 14 - Spr 04 Pseudocode for loop Get user input sum = sum + input Average sum Repeated statements in pseudocode become your loop 6

Michele Weigle - COMP 14 - Spr 04 Body of our loop Get user input sum = sum + input

Initializing Statements Michele Weigle - COMP 14 - Spr 04 Initializing Statements sum = sum + input Variables in your loop must be initialized (set to a value) before the loop What is initialization of sum? What if we found the product? sum = sum * input 8

Michele Weigle - COMP 14 - Spr 04 Ending a loop If you know number of loop iterations? Count-controlled loops for(count = 0; count < iterations; count++) User controlled ending Ask-before-iterating Sentinel value Booleans 9

Count-Controlled Loops Michele Weigle - COMP 14 - Spr 04 Count-Controlled Loops for(count = 0; count < iterations; count++) { System.out.print(“I have iterated “ + (count + 1) + “times\n”); }

Ask-Before-Iterating Michele Weigle - COMP 14 - Spr 04 Ask-Before-Iterating do { //do stuff in your code here System.out.print( “Continue? yes/no”); answer = keyboard.next(); } while (answer.equalsIgnoreCase(“yes”)); Do this on board 11

Michele Weigle - COMP 14 - Spr 04 Sentinel Value Signal end of input System.out.print(“enter a negative number to end the loop”); next = keyboard.nextInt(); sum = 0; while ( next >= 0 ) { sum = sum + next; System.out.print(“enter a number”); } 12

Michele Weigle - COMP 14 - Spr 04 Booleans int next, sum = 0; boolean numbersLeft = true; Scanner keyboard = new Scanner(System.in); while (numbersLeft) { next = keyboard.nextInt(); if (next < 0) numbersLeft = false; else sum = sum + next; } System.out.print(“the sum is “ + sum); 13

Michele Weigle - COMP 14 - Spr 04 What is the output? int count, count2; for (count = 0; count <= 3; count++) for(count2 = 0; count2 < count; count2++) System.out.println(count2); Count = 1 4 2 3 Count2 = 3 1 2 14

Michele Weigle - COMP 14 - Spr 04 Writing code Give a Java loop statement that will set the variable result equal to 25. 15

Michele Weigle - COMP 14 - Spr 04 Bugs Problem with program preventing correct execution Two most common mistake in loops Off-by-one errors Infinite Loops!!!!!! Grace Hopper 1947 Moth found in program Debugging! 16

Michele Weigle - COMP 14 - Spr 04 Off-by-one errors Loop repeats one too many or one too few times for (count = 1; count < 10; count++); Loop 9 times Do this on board 17

Infinite Loops A loop which repeats without ever ending is called an infinite loop. If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.

Michele Weigle - COMP 14 - Spr 04 Infinite Loops count = 1; while (count <= num) { System.out.print(count + “, “); //count++; } Do this on board 19

Michele Weigle - COMP 14 - Spr 04 Infinite Loops count = 1; while (count <= num); { System.out.print(count + “, “); count++; } Do this on board 20

Michele Weigle - COMP 14 - Spr 04 Infinite Loops int count; // initializing action; boolean expression; update action for (count = 1; count >= num; count++) { System.out.print(count + “, “); } Do this on board 21

Michele Weigle - COMP 14 - Spr 04 Finding errors Error checking System.out.print(variable); Run on simple input Debugger Do this on board 22

Michele Weigle - COMP 14 - Spr 04 Wednesday Program 2 Due Review of Strings and Loops 23