Repetition Structures Chapter 9

Slides:



Advertisements
Similar presentations
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Advertisements

Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Repeating Actions While and For Loops
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
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,
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Matlab for Engineers Logical Functions and Control Structures Chapter 8.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 5: Structured Programming
CNG 140 C Programming (Lecture set 3)
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Topics Introduction to Repetition Structures
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.
CS1371 Introduction to Computing for Engineers
Chapter 5: Control Structure
Chapter 4 – Control Structures Part 1
Topics Introduction to Repetition Structures
Web Programming– UFCFB Lecture 16
Lecture 4B More Repetition Richard Gesick
User Defined Functions
Selection By Ramin && Taimoor
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Types of Flow of Control
Introduction to Problem Solving and Control Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
UMBC CMSC 104 – Section 01, Fall 2016
Loop Statements & Vectorizing Code
Introduction to Repetition Structures
Computer programming Lecture 3.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Topics Introduction to Repetition Structures
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loop Statements & Vectorizing Code
Conditional Loops Counted Loops
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Repetition Structures Chapter 9

Structures Sequence Selection Repetition Sequence Selection Repetition (Loop)

Types of Loops Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for while

9.1 For Loops for index = [matrix] commands to be executed end The loop is executed once for each element of the index matrix identified in the first line

Check to see if the index has been exceeded True; You’ve run out of values in the index matrix Check to see if the index has been exceeded Flow chart for a for loop Calculations

Here’s a simple example the index can be defined using any of the techniques we’ve learned In this case k is the index – the loop is repeated once for each value of k

Here’s a simple example the index can be defined using any of the techniques we’ve learned In this case k is the index – the loop is repeated once for each value of k

One of the most common ways to use a loop is to define a matrix

Another use is in combination with an if statement to determine how many times something is true Each time through the loop we evaluate a single element of the scores matrix

While Loops While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met. while criterion commands to be executed end

We have to increment the counter (in this case k) every time through the loop – or the loop will never stop!!

This loop creates the matrix a, one element at a time

This program counts how many scores in the array are greater than 90, and displays the result in the command window

Hint If you accidentally create a loop that just keeps running you should Confirm that the computer is actually still calculating something by checking the lower left hand corner of the MATLAB window for the “busy indicator” Make sure the active window is the command window and exit the calculation manually with ctrl c

This program prompts the user 10 times to enter a value, and computes the natural log. If a negative value is entered, the break command causes MATLAB to exit the loop

Notice that n had a value of 3 when the program terminated – if it had run to completion it would have had a value of 10

The continue command is similar to break, however instead of terminating the loop, the program just skips to the next pass.

Notice that n had a value of 10 when the program terminated

9.4 Midpoint Break Loops Implemented with either a for or a while loop The loop is Entered Calculations processed Decision is made at an arbitrary point whether or not to continue

Results in a continuous loop Causes the loop to terminate

Limits the number of repetitions

9.5 Nested Loops It is often useful to nest loops inside of other loops To illustrate, consider code to mimic the built-in max function

9.6 Improving the Efficiency of Loops In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.

These two lines of code start a timer to measure the elapsed time required to run the lines of MATLAB code between them Here’s an example. This code creates a 4,000,000 element matrix of ones, then multiplies each element in the matrix by pi The amount of time it takes to run this code will depend on your computer, and how much else you have installed on it

This code accomplishes the same thing with a for loop

If we predefine the B matrix and then replace each element one at a time with the new value, the run time is significantly reduced Most of the time required to run the previous problem was used to create the B matrix one element at a time

An alternate syntax for timing uses the tic and toc functions

Hint Be sure to suppress intermediate calculations when you use a loop. Printing those values to the screen will greatly increase the amount of execution time. If you are brave, repeat the example above, but delete the semicolons inside the loop just to check out this claim. Don’t forget that you can stop the execution of the program with ctrl c. (But the command window needs to be active)