A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN 978-0-13-216675-1 Chapter 13 Conditional.

Slides:



Advertisements
Similar presentations
CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
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.
CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
5.05 Apply Looping Structures
The University of Texas – Pan American
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 12: How Long Can This Go On?
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Algorithm Design.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 5 JavaScript.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
CPS120 Introduction to Computer Science Iteration (Looping)
1 CSC 221: Computer Programming I Fall 2011 Python control statements  operator precedence  importing modules  random, math  conditional execution:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 7:
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University Conditional Execution.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Programming Fundamentals
JavaScript: Control Statements.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Iteration: Beyond the Basic PERFORM
Chapter 6: Repetition Statements
Chapter 11 Conditional Execution
Loops and Iteration CS 21a: Introduction to Computing I
Chapter 13 Conditional Repetition
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Computers and Scientific Thinking David Reed, Creighton University
Presentation transcript:

A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional Repetition 1

an if statement is known as a control statement it is used to control the execution of other JavaScript statements provides for conditional execution is useful for solving problems that involve choices  either do this or don't, based on some condition (if)  either do this or do that, based on some condition (if-else) 2 closely related to the concept of conditional execution is conditional repetition many problems involve repeating some task over and over until a specific condition is met e.g., rolling dice until a 7 is obtained e.g., repeatedly prompting the user for a valid input in JavaScript, while loops provide for conditional repetition

While Loops a while loop resembles an if statement in that its behavior is dependent on a Boolean condition. however, the statements inside a while loop’s curly braces (a.k.a. the loop body) are executed repeatedly as long as the condition remains true general form: while (BOOLEAN_TEST) { STATEMENTS_EXECUTED_AS_LONG_AS_TRUE } 3 when the browser encounters a while loop, it first evaluates the Boolean test if the test succeeds, then the statements inside the loop are executed in order, just like an if statement once all the statements have been executed, program control returns to the beginning of the loop the loop test is evaluated again, and if it succeeds, the loop body statements are executed again this process repeats until the Boolean test fails

While Loop Example example: roll two dice repeatedly until doubles are obtained 4 sample output: note: even though while loops and if statements look similar, they are very different control statements an if statement may execute its code once or not at all a while loop may execute its code an arbitrary number of times (including not at all)

Dice Roller Page 5

Avoiding redundancy 6 note the redundancy in the code must perform the initial dice roll before the loop begins then, have to repeatedly re-roll inside the loop can avoid this by either: "priming the loop" with default values that allow the loop to execute defining a Boolean "flag" to determine when the loop should continue

Loop Tests note: the loop test defines the condition under which the loop continues this is often backwards from the way we think about loops e.g., read input until you get a positive number (i.e., until input > 0) while (input <= 0) {... } e.g., keep rolling dice until you get doubles (i.e., until roll1 == roll2) while (roll1 != roll2) {... } e.g., keep rolling dice until you get double fours (i.e., until roll1 == 4 && roll2 = 4) while (roll1 != 4 || roll2 != 4) {... } 7 DeMorgan's Law: !(X && Y) == (!X || !Y) !(X || Y) == (!X && !Y)

Counter-Driven Loops since a while loop is controlled by a condition, it is usually impossible to predict the number of repetitions that will occur e.g., how many dice rolls will it take to get doubles? a while loop can also be used to repeat a task some fixed number of times implemented by using a while loop whose test is based on a counter general form of counter-driven while loop: repCount = 0; while (repCount < DESIRED_NUMBER_OF_REPETITIONS) { STATEMENTS_FOR_CARRYING_OUT_DESIRED_TASK repCount = repCount + 1; } the counter is initially set to 0 before the loop begins, and is incremented at the end of the loop body  the counter keeps track of how many times the statements in the loop body have executed  when the number of repetitions reaches the desired number, the loop test fails and the loop terminates 8

Counter-Driven Loops 9 examples :

Counter- driven Dice Roller 10 while loop executes totalRolls times  each time, the dice are rolled and checked for doubles

Infinite Loops the browser will repeatedly execute statements in the body of a while loop as long as the loop test succeeds (evaluates to true) it is possible that the test will always succeed and the loop will run forever repCount = 0; while (repCount < 10) { document.getElementById('outputDiv').inerHTML = document.getElementById('outputDiv').inerHTML + 'HOWDY '; } a loop that runs forever is known as an infinite loop (or a black hole loop) to guard against infinite loops, make sure that some part of the loop test changes inside the loop  in the above example, repCount is not updated in the loop so there is no chance of terminating once the loop starts an infinite loop may freeze up the browser  sometimes, clicking the Stop button will suffice to interrupt the browser  other times, you may need to restart the browser 11

Variables and Repetition any variable can be employed to control the number of loop repetitions and the variable can be updated in various ways example: countdown 12

Countdown Page 13

Example: Hailstone Sequences an interesting unsolved problem in mathematics: hailstone sequence 1. start with any positive integer 2. if the number is odd, then multiply the number by three and add one; otherwise, divide it by two 3. repeat as many times as desired for example: 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, … 14 it is conjectured that, no matter what positive integer you start with, you will always end up in the loop this has been verified for all starting number up to 5,764,607,523,034,234,880 but, it still has not been proven to hold for ALL starting numbers