Review while loops Control variables Example Infinite loop

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
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.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Loops and Files.
1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1. Definition and General Structure 2. Small Example 1 3. Simplified Structure 4. Short Additional Examples 5. Full Example 2 6. Common Error The for loop.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Variety of JavaScript Examples Please use speaker notes for additional information!
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Data Validation while loops. Data validation Programs get input data from different sources All data should be ‘validated’ before the program tries to.
Copyright © Curt Hill Loop Types and Construction Logical types and construction hints.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Fundamental Programming Fundamental Programming More on Repetition.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Repetition Structures The long awaited …. Repetition Structures I repeat …
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Learning Javascript From Mr Saem
Chapter 6 Controlling Program Flow with Looping Structures.
Iteration: FOR, DO, LOOP Loop Damian Gordon. FOR Loop The FOR loop does the same thing as a WHILE loop but is easier if you are using the loop to do a.
Loops & More 1.Conditionals (review) 2.Running totals and Running products 3.the for loop, examples 4.Nesting for loops, examples 5.Common errors 1.
Repetition Structures Chapter 9
Lesson 05: Iterations Class Chat: Attendance: Participation
Computer Programming I
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Scripts & Functions Scripts and functions are contained in .m-files
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
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.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Repetition Structures
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Do … Loop Until (condition is true)
Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
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.
Iteration – While Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Presentation transcript:

Review while loops Control variables Example Infinite loop Trap user while invalid input Ask first, then loop Hardcode bad, then loop

1. A control variable Any loop has a starting point and an ending point. In programming, a “loop control variable” keeps track of where we are. This variable has 3 phases: Initialization of variable – starting point Change of variable - incrementation Condition to exit loop – ending point

2. An example Example: the loop control variable is x while (x < 10)  2. condition x = x + 1; fprintf(‘%02d\n’, x); end The loop starts at zero, and while x is strictly less than 10, x is incremented by 1 and displayed to the screen. The loop stops when x actually reaches 10!

3. Infinite loop Infinite Loop  Loop control variable never meets the condition and the code runs and runs……. Use CTRL+C in the command window to break it

Infinite loop, cont. Why is this an infinite loop in MATLAB? x = 11; % 1. initialization while (x > 10) % 2. condition fprintf(‘%02d\n’, x); x = x + 1; % 3. change loop variable end

4. Trap a user for a value Ask a user for a decimal value between 1.0 to 10.0 What if they don’t give the proper value? Two approaches: Ask first, then loop Initialize the control variable to a bad value, and loop

4.1. Ask first, then loop (1) Ask the use first for a value (Give the user a chance to NOT mess up!) Then loop while it was a wrong value. %ask user for a specific distance distance = input(‘Enter a distance (1-10 cm): ’); %while this value was invalid, prompt again while (distance<1 || 10<distance) distance = input(‘Error! Enter a value 1-10: ’); end

4.1. Ask first, then loop (2) Ask the use first for a value (Give the user a chance to NOT mess up!) Then loop while it was a wrong value. %ask user for a specific distance distance = input(‘Enter a distance (1-10cm): ’); %while this value was invalid, prompt again while ~(1<= distance && distance<=10) distance = input(‘Error! Enter a value 1-10: ’); end

4.2. Make bad, then loop (1) The trick is to make MATLAB think a bad value has been entered, without prompting the user!!  HARDCODE %makes loop run at least once distance = -1; %BAD VALUE %prompt/repeat while invalid while (distance<1 || 10<distance) distance = input(‘Enter a distance (1-10 cm): ’); end This option does not allow an easy ERROR message.

4.2. Make bad, then loop (2) The trick is to make MATLAB think a bad value has been entered, without prompting the user!!  HARDCODE %makes loop run at least once distance = -1; %BAD VALUE %prompt/repeat while invalid while ~(1<= distance && distance<=10) distance = input(‘Enter a distance (1-10 cm): ’); end This option does not allow an easy ERROR message.

Wrapping Up while loops are very common to validate user-inputs. The block of code in a while loops only runs when the condition is true. The code block must change the condition, or it is called an infinite loop. A control-loop variable keeps track of whether the loop should execute or not