For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.

Slides:



Advertisements
Similar presentations
Array Operations ENGR 1181 MATLAB 4. Aerospace Engineers use turbulence data to calculate how close other planes can fly near the wake of a larger plane.
Advertisements

Nested Loops and the Break Statement. What are Nested Loops? Nested loops are: Loops which run inside another loop When would you use nested loops? Performing.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Flow Charts, Loop Structures
CMPS 1371 Introduction to Computing for Engineers
Program Design ENGR 1181 MATLAB 11. MATLAB Program Design in Real Life Complex manufacturing processes require careful logic mapping to determine what.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
MatLab – Palm Chapter 4, Part 3 For and While Loops
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Functions 1 ENGR 1181 MATLAB 14.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
Conditional Statements ENGR 1187 MATLAB 7. Conditional Statements in Real Life A Fluorescence Activated Cell Sorter (FACS) sorts cells based on predetermined.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
read and learn from example loop programs develop modular program
2D Plots 2 ENGR 1181 MATLAB 13. Plotting in the Real World 2D plots generated by MATLAB can be used in a variety of fields, including the one shown here,
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.
While Loops ENGR 1181 MATLAB 10.
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Matlab Programming for Engineers
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Array Operations ENGR 1181 MATLAB 4.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Working with Loops, Conditional Statements, and Arrays.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Array Accessing and Strings ENGR 1187 MATLAB 3. Today's Topics  Array Addressing (indexing)  Vector Addressing (indexing)  Matrix Addressing (indexing)
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CSE123 - Lecture 4 Structured Programming- Loops.
Logical Expressions ENGR 1181 MATLAB 6. Logical Expressions in Real Life Sorting objects in manufacturing processes can be accomplished automatically.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
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.
Computer Application in Engineering Design
CHAPTER 5A Loop Structure
Chapter 4 MATLAB Programming
CS1371 Introduction to Computing for Engineers
Siti Nurbaya Ismail Senior Lecturer
MATLAB – Basic For Loops
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Iteration: Beyond the Basic PERFORM
Types of Flow of Control
Array Creation ENGR 1181 MATLAB 02.
For Loops (Iteration 1) Programming Guides.
Islamic University of Gaza
CISC101 Reminders Quiz 1 marking underway.
Week 7: Computer Tools for Problem Solving and Critical Thinking
Presentation transcript:

For Loops 2 ENGR 1181 MATLAB 9

For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been a useful tool for completing mundane tasks over and over again. The focus of today’s lesson is fixed number looping although later you will learn about indefinite looping (while looping). Internal combustion engines use a combination of these loops for controlling the spark plugs and valves during a four stage cycle. The engine completes these four stages (for loop) while the car is engine is running indefinitely (while loop, to be learned later).

Today's Learning Objectives  After today’s class, students will be able to: Use more complex ways of setting the loop index. Construct nested loops in the following situations:  For use with two dimensional arrays  For repeating a calculation numerous times Use loops to repeat a code with conditional statements.

Review: For Loops for k=1:3:10 x=k^2 end x= 1 x= 16 x= 49 x= 100  Every for must have an end  Loops can be used in the command window or in script files  Loops can be nested (loops inside of other loops!)  if statements can be used inside of loops... and loops can be used inside if statements

Summing with For Loops  If you use a for loop to calculate a sum, the sum variable must be initialized to zero before the loop.  MATLAB needs to be reminded to start counting at 0 when it calculates a sum.  Remember to avoid using MATLAB function names as variable names! sum1=0; for k=2:2:10 sum1=sum1+k; end disp(sum1) sum1= 30

For Loops With If Statements  Conditional statements can be used within for loops  Just like nesting for loops, if statements and for loops can also be nested  Useful in many applications such as sorting data or displaying statements to the screen

Example  Given the vector v=[ ], use a loop to calculate the square of each element.  If the square is less than 40, display a message saying: “The square is..., which is less than 40.”  If the square is more than 40, display a message saying: “The square is..., which is greater than 40.”

Example Program v=[ ]; for k=1:4 vs=v(k)^2; if vs 40 fprintf(‘\nThe square is %i, which is greater than 40.’, vs) end end

Example Program Output The square is 4, which is less than 40. The square is 16, which is less than 40. The square is 36, which is less than 40. The square is 64, which is greater than 40.

The Break Command  The ‘break’ command will terminate a current loop.  MATLAB will jump to the end and carry on with the program.  Often used with if statements: If a certain condition is satisfied, the loop can be terminated.  See prep material for an example with ‘break’.  Type ‘help break’ in command window for more information

The Continue Command  The ‘continue’ command will stop the remainder of the current loop pass.  The program continues with the next iteration of the loop.  Often used with if statements: If a certain condition is satisfied, the pass can be stopped.  See prep material for an example of ‘continue’.  Type ‘help continue’ in command window for more information.

Important Takeaways  For loops and if statements can be used in combination by nesting  The ‘break’ command has MATLAB jump to the ‘end’ of the loop (breaks the loop)  The ‘continue’ command has MATLAB jump to the end of the iteration (doesn’t break the loop)

Preview of Next Class  Program Design What is an algorithm, how are they useful? Flowcharts Top Down Examples

What’s Next?  Review today’s Quiz #09  Open the in-class activity from the EEIC website and we will go through it together.  Then, start working on MAT-09 homework.  Prepare for next class by reading about While Loops