PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.

Slides:



Advertisements
Similar presentations
CSC 107 – Programming For Science. Todays Goal After today, should know why we use loops How to use while & do / while loops in code Know & explain when.
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.
CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Loops – While, Do, For Repetition Statements Introduction to Arrays
Objectives You should be able to describe:
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
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 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
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.
Algorithm Design.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
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.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
CSC Programming for Science Lecture 12: while & do/while Loops.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Chapter 4 Repetition Statements (loops)
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Looping and Repetition
Outline Altering flow of control Boolean expressions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 6: Repetition Statements
CHAPTER 21 LOOPS 1.
Decisions, decisions, decisions
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
ICS103: Programming in C 5: Repetition and Loop Statements
Looping and Repetition
Presentation transcript:

PHY 107 – Programming For Science

Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand loop termination conditions  Be able to write bounded and unbounded loops  Explain how for, while & do / while loops differ  Convert between the 3 loops with same end result  Explain why we would never convert between loops

Code Structures in Programming  Sequence  Decision  Repetition true false true false

Loop Structure

Loop Termination Condition  Most important detail of a loop  This is what tells computer when loop should execute  Errors common in loop termination condition  Computers are dumb & happily execute infinite loops  Waste lot of time waiting for this to finish EXACTLY  Remember: computers do EXACTLY what they're told  Have infinite patience with the stupid tasks we ask

Loop Body  Actions loop performs each repetition  Loop body completed before checking condition  Even  Even if variable reassigned no rechecking within body  May contain any code including another loop  Nested loops common in science & engineering  Finding and then fixing errors can be hard  Error may be small, but executed many times…  Always test out on paper to verify idea works, first

for Loop Structure

for Loop

for Loop Strengths  for loops good at bounding times loop is run  (Optional) Declare & initialize variable doing counts  Loop statement also clearly shows loop termination  (Optional) Variable‘s update made quite clear  May not always have a variable controlling update  Might want to execute only while some expression true  Update within loop body and not just at the end  Not just a single variable, but multiple variables needed

while Loop while (expression) { statement;... }  Evaluates expression to find its value  If true, executes entire loop body  Re-check after each pass through loop, only  Continue with loop while expression checks true

Tracing while Loop int mPerMi = 1; float mph; while (mPerMi > 1) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);

Tracing while Loop int mPerMi = 1; float mph; while (mPerMi >= 1) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);

Tracing while Loop int mPerMi = 1; float mph; while ((mPerMi >= 1) && (mPerMi < 4)) { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } printf("Now I know.\n“);

Sometimes you feel like a nut…  May want loop only if expression is true  while checks before entering loop to see if true  But may want to execute loop body at least once  do - while checks after executing of loop  Why use one and not the other?  Depends on when you want check to occur  Otherwise, there are not any real differences

do - while Loop

Sample do - while Loop int mPerMi = 1; float mph; do { mph=(1.0/mPerMi)*60.0; printf(“%d min/mile = ", mPerMi); mPerMi += 1; printf("%f mph\n", mph); } while (mPerMi > 1) ;

Your Turn  Get in groups & work on following activity

For Next Lecture