Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

While loops.
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.
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
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.
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.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition Statements while and do while loops
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Computing Concepts Note Set 14. What if… You had to print “I love Java” to the screen 125 times. How? 125 lines of ▫ System.out.println(“I.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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.
CE En 270 Brigham Young University Norm Jones
FOP: While Loops.
Control Flow (Python) Dr. José M. Reyes Álamo.
Chapter 4 Repetition Statements (loops)
EGR 2261 Unit 5 Control Structures II: Repetition
Shortcoming of the FOR-DO loop
Introducing Do While & Do Until Loops & Repetition Statements
Logical Operators and While Loops
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Looping and Repetition
Loops October 10, 2017.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Patterns to KNOW.
LOOPS BY: LAUREN & ROMEO.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Loop Strategies Repetition Playbook.
Computer Science Core Concepts
ICT Programming Lesson 3:
Logical Operators and While Loops
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
CS2011 Introduction to Programming I Loop Statements (I)
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
REPETITION Why Repetition?
Looping and Repetition
Presentation transcript:

Shortcoming of the FOR-DO loop When you use the FOR-DO loop you must know how many times you want to perform an action. What if you didn’t know how many times an action was to be performed? For example, suppose you wanted to know how many times an action needed to be repeated before a certain condition was met.

The WHILE loop WHILE (some Boolean expression is true) DO some particular action i := 0;{initialization of i} WHILE i < 100 DO BEGIN Write (i, ‘ ‘); i = i + 3; End;

The WHILE loop (con’t) The identifiers in the Boolean expression that controls the WHILE loop must all be defined. If they are not defined by an earlier part of the program, you must initialize them (the book calls this priming) before the loop begins.

The WHILE loop (con’t) At least one of the statements in the loop must change the value of the Boolean expression so it will eventually become false. Otherwise, the loop will execute forever (this is called an infinite loop). In Pascal we stop an infinite loop by pressing Ctrl-Break Poor logic can also cause an infinite loop.

Sentinels A sentinel is a piece of data that ends a set of data but isn’t a possible value of the data For example, if we write a program that a police officer uses to enter the speed of each car (s)he gave a speeding ticket to during a week, the end of the data can be indicated by a -1.