* 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.

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
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.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 5: Control Structures II (Repetition)
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
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.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
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
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
For Loops 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while Which loop to use? task with a specific.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
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.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
UCT Department of Computer Science Computer Science 1015F Iteration
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Control Structure Senior Lecturer
Control Statements Loops.
Control Statements Loops.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

* 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 until the numbers are greater than 1000 C. Output the first 100 multiples of 13

Pre-AP Computer Science, Cycle 6

* Loops are used for repeating tasks or sections of code * While loops are used when the number of times to loop is unknown, or may change * For loops are used when the number of times to loop is known, or can be represented by a variable

* “While this condition is true, do this” * “Do this until this is true / do this until this isn’t true” int x=0; int y; while (condition)while (x < 100){ //statements y = x*2; } x++; }

* “Repeat this ‘x’ times” * “Cycle through ‘x’ things” for (int i=0; i<100; i++) { int y=i*2; }

* Counter controlled loops are characterized by the use of a counter, or variable, that tracks the number of times a loop has executed * Some while loops are counter controlled, but ALL for loops are counter controlled

int x=0; int y; while (x < 100) { y = x*2; x++; } for (int i=0; i<100; i++) { int y=i/3; }

* Non-counter controlled loops do NOT use a counter to keep track of loop iterations * Number of iterations doesn’t matter * Non-counter controlled loops are ALWAYS while or do-while loops * Examples: sentinel-controlled; flag-controlled

* Sentinel: Special value that signal when the loop should stop running * Loop continues as long as the sentinel value is not encountered Structure while (variable != sentinel)

while (answer != “no”) { System.out.print(x); x++; System.out.print(“Keep looping?”); answer = console.next(); }

* Uses a boolean variable to control the loop * The boolean variable is called a flag * Must eventually be set to true in loop body Structure boolean flag = false; while (!flag)

boolean stop = false; while (!stop) { System.out.println(“banana!”); System.out.println(“More bananas?”); String ans = console.next(); if (ans == “no” || ans == “No”) { stop = true; }

boolean loop = true; while (loop) { System.out.println(“banana!”); System.out.println(“More bananas?”); String ans = console.next(); if (ans.charAt(0) == ‘n’) { loop = false; }

* For each of the following kinds of loops, state whether a FOR loop, WHILE loop, or both are capable of performing that kind of loop. A. Counter-Controlled B. Sentinel-Controlled C. Flag-Controlled

* List at least 10 steps that the computer would have to complete for the following problem: A program continues to ask the user to input numbers until they input they number -1. Then, the computer outputs the minimum and maximum of the numbers typed in.

* Set up variables for num, min, and max * Begin a loop that runs while num != -1 * Ask the user for a number * Save their response in “num” * IF num != -1 * Compare num to the minimum * Replace minimum with the smallest number * Compare num to the maximum * Replace max with the largest number * Output minimum * Output maximum

Pre-AP Computer Science, Cycle 6

* Counter Controlled * Loop ends when the counter expires * for (int counter=0; counter<100; counter++) * Sentinel-Controlled Loop * Loop ends when the sentinel value is encountered * while (answer != -1) * Flag-Controlled Loop * Loop ends when the flag (boolean) is reversed * while (!stop)

* Used for looping situations that MUST occur at least one time * Have the same expressive power as WHILE loops * Counter, flags, sentinels * Primary Difference: It checks the condition AFTER the loop statement

* “Do this while this is true” do { //statements } while (condition);

while (x != 0) { //statements; } do { //statements; } while (x !=0);

* Add up all user-inputted numbers until 0 is encountered do { System.out.print(“Enter a value: “); data = input.nextInt(); sum = sum + data; } while (data != 0); System.out.println(“Sum: “ + sum);

* Print random numbers between until the number 42 is encountered do { int num = Math.random()* ; System.out.println(num); } while (num != 42);

* Convert the following while loop into a do-while loop while (number != 15) { System.out.println(number); number = Math.random()*20+1; }

* Which loop would be best for the following prompts? FOR loop, WHILE loop, or DO-WHILE loop A. A program that displays all the numbers from 100 to 1000 that are divisible by 5 and 6 B. A program that allows the user to type in test scores until they type “STOP”

Pre-AP Computer Science, Cycle 6

* Can be used to immediately terminate a loop early * Can be used in any kind of loop * Use by merely writing break;

int sum = 0; int number = 0; while (number < 20) { number++; sum = sum + number; if (sum >= 100) { break; }

int number = (int)(Math.random()*100); while (true) { System.out.println(“Guess my number!); int guess = console.nextInt(); if (guess == number) { System.out.println(“Correct!”) break; } else if (guess > number) System.out.println(“Too high!”); else System.out.println(“Too low!”); }

* Can be used to immediately break out of the current iteration of a loop, and skip to the next iteration * Can be used to skip certain situations without having to use complex logic for your condition (&& and ||)

int number = 0; while (number < 5) { if (number == 3) { continue; } System.out.print(number + “ “); } Output:

* Typically, use of break and continue is discouraged as they can create loops with too many exit points that are difficult to debug * Break and continue should only be used if they simplify the loop, and make it easier to read