while Loops Looping Control Statement

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
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.
Repeating Actions While and For Loops
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops and Iteration for Statements, while Statements and do-while Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
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.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
FOP: While Loops.
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Loops and Files.
Topics Introduction to Repetition Structures
ITM 352 Flow-Control: 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.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
LRobot Game.
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Loops CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Introduction to Repetition Structures
Topics Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Java LESSON 3 Loops.
PROGRAM FLOWCHART Iteration Statements.
The for-statement.
Python While Loops.
CSC1401 Manipulating Pictures 2
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Chapter 13 Conditional Repetition
Lecture 3 More on Flow Control, More on Functions,
Chapter 3 Flow of Control Loops in Java.
Functions Overview © 2018 Kris Jordan.
Visual Studio Code Walkthrough
Looping and Repetition
The main Function, introcs Library, and Prompting
if-then-else Conditional Control Statement
The return Statement © 2018 Kris Jordan.
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Arrays: Iteration Working through an array algorithmically.
Presentation transcript:

while Loops Looping Control Statement © 2018 Kris Jordan

Introducing: while Loops General form of a while loop statement: while (<boolean expression "test">) { <repeat block – statements in braces run when test is true> } Like an if-then statement: the test you place in the parenthesis must be a boolean expression if the test evaluates to true, the computer will move to the first line of code in the repeat block If the test evaluates to false, the computer will jump over the repeat block Important! Unlike an if-then, after the last statement in the repeat block completes, the computer will next jump backwards up to the test and start afresh. A while loop statement can be used anywhere you can write a statement.

while loop Flow of Control test When a while statement is encountered, its boolean test expression is evaluated If the test is true, then the processor will proceed into the repeat block. At the end of the repeat block, the processor jumps back to step 1. If the test is false, the processor will jump over the repeat block and continue on. false repeat true

Example Setup In VSCode: Start the Development Server import { print, promptNumber } from "introcs"; export let main = async () => { let n = await promptNumber("How many times?"); let i = 0; while (i < n) { print("Loop: " + i); i = i + 1; } print("Done!"); }; main(); In VSCode: Start the Development Server View Terminal npm run pull npm start Open the File Explorer Pane Right click on the src folder Select "New folder" Name it: x-while Right click on the x-while folder Select "New file" Name it: while-app.ts In while-app.ts, write out the code to the right. It has no errors, so review carefully if yours has any. © Kris Jordan

Writing a while loop that repeats a specific number of times. 1 Repeating a task a specific number of times is a very common task in computing. You will see this all semester. Three keys: Declare a counter variable and initialize it to 0. The loops test will check that the counter variable is less than the # of times you want to repeat Don't forget! The last step inside of the repeat block is incrementing your counter variable. let i = 0; while (i < ____) { // Do Something Useful i = i + 1; } 2 3

while loop Statement Notes If the test is not true the first time the while loop is encountered, then the computer will jump past the repeat block. If the test never evaluates to false, then the loop is called an infinite loop. The only way to stop an infinite loop is to force quit/close your browser. test false true repeat

How do you avoid infinite loops? Your test condition must eventually evaluate to false, therefore a value in the test must be changing inside the repeat block, such that progress is made toward the test expression evaluating to false. let i = 0; while (i < n) { print("Loop!"); } Bad! Nothing is changing inside of the repeat block. let i = 0; while (i < n) { print("Loop!"); i = i - 1; } Bad! Subtracting 1 from i is not making progress toward i >= n. let i = 0; while (i < n) { print("Loop!"); i = i + 1; } Good! Adding 1 to i is making progress toward i >= n.