COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips 367 1.

Slides:



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

Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
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 More Loops and Strings Tabitha Peck M.S. February 18, 2008 MWF 3-3:50 pm Philips
1 COMP 110 Arrays Tabitha Peck M.S. March 31, 2008 MWF 3-3:50 pm Philips 367.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 If / Else and Case Statements Tabitha Peck M.S. February 4, 2008 MWF 3-3:50 pm Philips
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
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.
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
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
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 Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
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.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
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,
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?
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
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.
COMP Loop Statements Yi Hong May 21, 2015.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.
Loops.
Chapter 5: Control Structures II
Loop Structures.
Java's for Statement.
Computer Programming Methodology Input and While Loop
Repetition-Counter control Loop
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.
COMP 110 Loops, loops, loops, loops, loops, loops…
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Presentation transcript:

COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips 367 1

Announcements Office hours THIS WEEK ONLY! Monday 4:00-5:00 Tuesday 1:30-2:30 in Sitterson 252!!!!!!! Program 2 Due Wednesday by 3:00 Grades online 2

Questions? 3

Today in COMP 110 Loops Body Initializing Statements Ending a loop Bugs 4

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

Pseudocode for loop Get user input sum = sum + input Get user input sum = sum + input Get user input sum = sum + input Average sum 6 Repeated statements in pseudocode become your loop

Body of our loop Get user input sum = sum + input

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

Ending a loop 9 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

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

Ask-Before-Iterating do { //do stuff in your code here System.out.print( “Continue? yes/no”); answer = keyboard.next(); } while (answer.equalsIgnoreCase(“yes”)); 11

Sentinel Value 12 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”); next = keyboard.nextInt(); }

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

What is the output? int count, count2; for (count = 0; count <= 3; count++) for(count2 = 0; count2 < count; count2++) System.out.println(count2); 14 Count = Count2 =

Writing code Give a Java loop statement that will set the variable result equal to

Bugs Infinite loops Off-by-one errors 16

Infinite Loops count = 0; while (balance < 0) { balance = balance - penalty; balance + deposit; count++; } System.out.print(“You will have a nonnegative” + “balance in “ + count + “days”); 17

Off-by-one errors Loop repeats one too many or one too few times for (count = 1; count < 10; count++); Loop 9 times 18

Finding errors Error checking System.out.print(variable); Run on simple input Debugger 19

Wednesday Read 4.1 Program 2 Due Office hours Today after class and Tuesday at 1:30 20