CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.

Slides:



Advertisements
Similar presentations
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Advertisements

CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 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.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Control Structures II (Repetition)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5 Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Repetition Statements while and do while loops
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 M.A. Papalaskari, Villanova University Algorithms Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Flow of Control (2) : Loops Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 4 Repetition Statements (loops)
CSC 1051 – Data Structures and Algorithms I
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Outline Altering flow of control Boolean expressions
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Alternate Version of STARTING OUT WITH C++ 4th Edition
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus

Control flow Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow CSC 1051 M.A. Papalaskari, Villanova University statement 1 statement 2 statement 3 statement 4 boolean 1boolean 2 statement 1 statement 2 true false statement 3 This slide dapted from Doug Clark’s course Review

Example Investment problem: You put $10,000 into a bank account that earns 5% interest per year. … How many years does it take for the account balance to be double the original? CSC 1051 M.A. Papalaskari, Villanova UniversityThis example is adapted from Cay Horstmann’s Big Java, Early Objects, 5 th edition

Example Investment problem: You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original? Algorithm: CSC 1051 M.A. Papalaskari, Villanova University

The while Statement A while statement has the following syntax: while ( condition ) statement; If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false CSC 1051 M.A. Papalaskari, Villanova University

Logic of a while Loop statement true false condition evaluated CSC 1051 M.A. Papalaskari, Villanova University

Example A counting loop that prints the numbers 1, 2, 3,… Algorithm: initialize a counter to 1 while the counter <= upper limit –print counter –increment counter CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Initialize count count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 Output: 1 count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 Output: 1 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 2 count 2 Output: 1 Output: 1 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 Output: 1 2 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 Output: 1 2 count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 3 count 3 Output: 1 2 Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: Output: count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: Output: count 4 count 4 CSC 1051 M.A. Papalaskari, Villanova University

The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is false count 4 count 4 Output: Output: CSC 1051 M.A. Papalaskari, Villanova University

The while Statement “unraveled” int count = 1; while (count <= 3) { System.out.println(count); count++; } int count = 1; while (count <= 3) { System.out.println(count); count++; } Output: Output: CSC 1051 M.A. Papalaskari, Villanova University int count = 1; TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  false EXIT LOOP int count = 1; TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  true { System.out.println(count); count++; } TEST :(count <= 3)  false EXIT LOOP count 4 count 4 count 3 count 3 count 2 count 2 count 1 count 1

What’s wrong with this code? int count = 1; while (count <= 10) System.out.println (count); count++; CSC 1051 M.A. Papalaskari, Villanova University

What’s wrong with this code? int count = 1; while (count <= 10); { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University

Example Table of powers: Compute the powers of 2 and the powers of 3 and print a table like this: CSC 1051 M.A. Papalaskari, Villanova University

If the condition of a while loop is false initially, the statement is never executed int count = 8; while (count <= 3) { System.out.println (count); count++; } Therefore, the body of a while loop will execute zero or more times CSC 1051 M.A. Papalaskari, Villanova University

Example: Input validation – First try System.out.println(“Enter lifestyle code”); System.out.println (“0=bad; 1=ok; 2=super fit”); int lifestyle = scan.nextInt(); if (lifestyle 2) { System.out.println (“Please try again”); System.out.println (“0=bad; 1=ok; 2=super fit”); num = scan.nextInt(); } CSC 1051 M.A. Papalaskari, Villanova University

Correction: May 2, 2013 An earlier version of this article referred incorrectly to the products sold at By Brooklyn. The store does not sell dandelion and burdock soda, lovage soda syrup, and Early Bird granola “gathered in Brooklyn.” An earlier version also referred incorrectly to the thoroughfare that contains the thrift shop Vice Versa. It is Bedford Avenue, not Bedford Street, or Bedfoprd Avenue, as stated in an earlier correction. New York Times "How I Became a Hipster" Published: May 1, CSC 1051 M.A. Papalaskari, Villanova University Sometimes people need more than a second chance (a third? a fourth?...) how about giving the user more chances: allow them to repeatedly enter the input while they are getting it wrong what is a small change we can make on the previous example to effect that?

What if we want to do a calculation over and over again? Example: Calculating GPA for many students (how many? when do you stop?) Possible approaches: Keep accepting new inputs (for each student) and calculating and printing corresponding GPA until user quits program (infinite loop). Same, but ask each time whether to keep going. Same, but quit if the user inputs -1 for the credits (signals end) Calculate GPA for exactly 20 students CSC 1051 M.A. Papalaskari, Villanova University

Nested loops Example: Investment problem repetition  the repeated action (calculating the number of years it take for investment to double) involves repetition General pattern for algorithms: A nested loop while (condition for repeating action) initialize variables (?) while (condition for reaching goal) calculations print results CSC 1051 M.A. Papalaskari, Villanova University

Homework Read Section 5.4, –the example of Nested loops (PalindromeTester.java, pp ) uses some concepts we have not covered, so you can skip that for now). –Always do all self-review exercises when you review material Do end of chapter Exercises EX 5.7 – 5.11 CSC 1051 M.A. Papalaskari, Villanova University