CSC 107 - Programming for Science Lecture 12: while & do/while Loops.

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

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.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
CSC Programming for Science Lecture 11: Switch Statements & ?: Operator.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Repetition Structures: Do-while Loop Random Number Generation CSC 1401: Introduction to Programming with Java Week 6 Wanda M. Kunkle.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CSC103: Introduction to Computer and Programming
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CSC 107 – Programming For Science. Announcement Today’s Goal  Know how to write selections besides if-else  Why we use select or if - else and how.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. 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;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
CPS120 Introduction to Computer Science Iteration (Looping)
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
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.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
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.
COMP Loop Statements Yi Hong May 21, 2015.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
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.
CSC Programming for Science Lecture 5: Actual Programming.
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.
Lecture 7: Repeating a Known Number of Times
Quick Test What do you mean by pre-test and post-test loops in C?
The nested repetition control structures & Continue Statements
Arrays, For loop While loop Do while loop
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
LRobot Game.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
MSIS 655 Advanced Business Applications Programming
Repetition Control Structure
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
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.
Repetition Structures
Looping Structures.
Looping and Repetition
Presentation transcript:

CSC Programming for Science Lecture 12: while & do/while Loops

Today’s Goal After today, should know why we use loops and how to use while and do/while loops work  Know when use of either loop is preferable  Understand loop termination conditions  Write bounded and unbounded loops

Program Structure All code so far has been linear  Runs through code in nearly sequential order  Perform actions at most once  Ignores the main benefit of computers Computers are fast, but dumb  (NO drawing parallels to any person live or dead)  Best performing simple * tasks over-and-over  Repetitive, not sequential, execution

Loops Direct actions to be performed repetitively  Over 90% computer execution spent in loops Scientific & artistic programming loop-based  Simulate how system changes over time  Compute what each pixel on screen should contain  Block-by-block evaluation of sea waves  Analyzing traffic patterns throughout day All loops can be broken into two parts  Loop body  Loop termination/loop continuation condition

Loop termination/continuation Most important detail of a loop  Specifies when loop should execute Errors in loop termination very common  Infinite loop continues like scratched CD  Does same thing over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and …

 over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over over and over and over and over and over and over and … Waste a lot of time waiting for these programs to finish

Loop Body Actions loop performs each repetition Can very simple  PowerPoint blinking cursor waiting for me to type Can be very complex  Simulating U.S. weather for that moment Errors here can be hard to fix  Executed many times  Error can spread as loop repeats  First test on paper & verify it works  Check that small errors cannot grow with repetition

while Loop while (expression) { statement;... } Evaluates the “boolean” expression If “true”, execute entire loop body  After loop body completes, check expression Remains in loop while expression is “true”

Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 1) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi += 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 0) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi += 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

Tracing while Loop 0 int mPerMi = 1; 1 float mph; 2 while (mPerMi > 0) { 3 mph=(1.0/mPerM)*60.0; 4 printf(“%d min/mile”, mPerMi); 5 printf(“ = %lf mph\n”, mph); 6 mPerMi -= 1; 7 } 8 printf(“Now I know\n”); Line #mPerMimph

Sometimes you feel like a nut… Sometimes should execute loop only if loop continuation expression was “true”  while performs check before executing loop Sometimes want to execute loop body at least once  do-while checks loop continuation expression after each execution of loop Only difference between while & do- while

do-while Loop do { statement;... } while (expression); Execute entire loop body  Then evaluate the “boolean” expression If “true”, re-execute entire loop body  After loop body, recheck expression Unlike all other expressions, semicolon needed

Your Turn Get into groups and complete daily activity

For Next Lecture Keep reading Sections 3.4 of book  Do not need to understand all the details  But important knowing what is not understood Start week #5 weekly assignment Finish lab #3 First programming assignment on web  Due 1 week from Friday