MatLab – Palm Chapter 4, Part 3 For and While Loops

Slides:



Advertisements
Similar presentations
Introduction to Flowcharting
Advertisements

Introduction to Flowcharting
CMPS 1371 Introduction to Computing for Engineers
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Loops and Files.
ENGR 111A - Spring MatLab – Palm Chapter 4, Part 4 Review and Debugging Class 12.1: Palm Chapters &
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
(C)opyright 2003 Scott/Jones Publishers Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Scott/Jones Publishers.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
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.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
JavaScript, Fourth Edition
ENGR-25_Programming-4.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE Licensed Electrical.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Fortran: Control Structures Session Three ICoCSIS.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
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.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
EE 201 1C10-2 Spring 2012 LOOPS For.  Achieve Comprehension LOL of using Loops in programming. Class Learning Objectives 2C10-2 Spring 2012.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Introduction To Repetition The for loop
Repetition Structures Chapter 9
The switch Statement, and Introduction to Looping
CS1371 Introduction to Computing for Engineers
Think What will be the output?
CiS 260: App Dev I Chapter 4: Control Structures II.
The Linux Command Line Chapter 29
Chapter 5 Repetition.
Programming Fundamentals Lecture #6 Program Control
Types of Flow of Control
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
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.
LOOPS For EE 201 C10-1 SPRING 2012.
REPETITION Why Repetition?
Presentation transcript:

MatLab – Palm Chapter 4, Part 3 For and While Loops Class 11.1 Palm Ch 4.5 4/5/2004 ENGR 111A - Spring 2004

RAT 11.1 As in INDIVIDUAL you have 2 minutes to answer the following question. What is the FINAL value assigned to y in the following segment of MATLAB code? x = 0; while x < 5 y = sqrt(x); x = x + 1; end When asked, you have 30-seconds to submit your paper. Answer: y = 2 4/5/2004 ENGR 111A - Spring 2004

Learning Objectives Students should be able to: Understand Matlab iteration structures: for … end Loops while … end Loops Identify Implied loops Recognize these structures as repetition (or iteration) structures. Develop flow (logic) diagrams for combinations of branching and looping structures 4/5/2004 ENGR 111A - Spring 2004

LOOPS A LOOP is a structure for repeating (iterating) a block of code. MATLAB uses two types of loops. for loop: number of passes known a priori (p. 210). while loop: loop terminates when a specified condition is satisfied (p. 221). 4/5/2004 ENGR 111A - Spring 2004

for LOOPS SYNTAX: for k = start:step:stop block end start = initial value step = incremental value stop = terminating value See p. 212 for flowchart of for loop. Indentation Mandatory! 4/5/2004 ENGR 111A - Spring 2004

for LOOP FLOW CHART Loop structure 4/5/2004 ENGR 111A - Spring 2004

EXAMPLE for k = 5:10:35 x = k^2 end 4/5/2004 ENGR 111A - Spring 2004

for … end Loops (p. 211) for k = 5:10:35 x = k^2 end Means: starting with k = 5, execute the statements between for … end; then increment k by 10 and execute the statements with k = 15; etc. until k > 35. Then exit the loop (k=? at this point) 4/5/2004 ENGR 111A - Spring 2004

for … end Loops cont’d. Step value may be negative (default is positive 1) For step > 0, loop does not execute if start > stop For step < 0, loop does not execute if start < stop If start = stop, loop executes once Non-integer step values give unpredictable results (don’t use them!) 4/5/2004 ENGR 111A - Spring 2004

INDIVIDUAL EXERCISE (5 min.) Evaluate xn = sin (np/10) for n between 1 and 10 (in increments of 1). Write a Matlab PROGRAM. (Plot x versus n) Save file on R:\ and name file after an historical figure. 4/5/2004 ENGR 111A - Spring 2004

SOLUTION for n = 1:10 x(n) = sin(n*pi/10); end plot(1:10,x) % or title(‘In-class… example’) xlabel(‘n’) ylabel(‘x(n)’) 4/5/2004 ENGR 111A - Spring 2004

for Loops for loops can be nested (one inside each other). Each loop is closed with an end. EXAMPLE: Proper indention is a MUST!! N = 10; for I = 1:N for J = 1:N A(I,J) = 1/(I + J – 1); end Why is I OK and i not OK? 4/5/2004 ENGR 111A - Spring 2004

for Loops for loops can be nested (one inside each other). Each loop is closed with an end. EXAMPLE: Proper indention is a MUST!! N = 10; for r = 1:N for c = 1:N A(r,c) = 1/(r + c – 1); end Using r (for row) and c (for col) is MUCH better!!! 4/5/2004 ENGR 111A - Spring 2004

In-class Assignment 11.1 (20 min.) Work through the nested loop and if example on page 212. Write an program file to solve T4.5-1 (p. 213) Save your solutions on the R:\ drive Name your files after an animal. 4/5/2004 ENGR 111A - Spring 2004

Solution % PROGRAM example1.m % % Solution to T4.5-1 on p. 213 of the Palm % Matlab book. % S. Socolofsky % ENGR 111A: 501-503 for r = 1:4 for c = 1:3 A(r,c) = 4 + (r-1)*6 + (c-1)*4; end 4/5/2004 ENGR 111A - Spring 2004

The “break” Statement You can EXIT out of any looping structure with a “break” (Both for… and while…) y=0; x=[1 -2 3 -4 5]; for k = 1:length(x); if x(k) < 0 break end y = y + sqrt(x(k)); % -- Jump to here on break 4/5/2004 ENGR 111A - Spring 2004

The “continue” Statement You can skip “bad values” in any looping structure with a “continue” y=0; x=[1 -2 3 -4 5]; for k = 1:length(x); if x(k) < 0 continue end y = y + sqrt(x(k)); end % Jump to here on continue 4/5/2004 ENGR 111A - Spring 2004

Implied Loops (p. 216) You can use built-in loops instead of for … end loops in most cases. Sometimes it is better, sometimes not. Note the matrix example with simple logic Note the for … end example with more complex logic Note the find function example for a very normal situation that you once upon a time had to write code for every time. 4/5/2004 ENGR 111A - Spring 2004

Implied Loops (cont.) Many MatLab commands contain implied loops. Example: x = [0:5:100]; y = cos(x); y = find(x>0); See p. 216 for equivalent loops for these. 4/5/2004 ENGR 111A - Spring 2004

while LOOPS The while loop terminates because a specified condition is satisfied. The number of passes is NOT known a priori SYNTAX: while logical expression statements end See p. 223 for flowchart of while loop. 4/5/2004 ENGR 111A - Spring 2004

while LOOP FLOW CHART Loop structure 4/5/2004 ENGR 111A - Spring 2004

while … end Loops Look at the examples on page 221. x = 5; while x < 25 disp(x) x = 2*x-1 %-make sure x changes end Means: starting with x = 5, execute the statements between the while … end; this displays x and calculates a new value of x and loops back. Then exit the loop when x exceeds or equals 25. 4/5/2004 ENGR 111A - Spring 2004

while … end Loops (p.222) THE LOOP VARIABLE MUST HAVE A VALUE BEFORE THE while STATEMENT IS EXECUTED THE LOOP VARIABLE MUST BE CHANGED SOMEHOW BY THE STATEMENTS Note the usual mistakes toward the bottom of the page. Make sure that you save your code before testing any while … end loop. 4/5/2004 ENGR 111A - Spring 2004

INDIVUDAL EXERCISE (5 min.) Write an equivalent while loop for the implied loop shown: x = [0:5:100]; y = sin(x); disp (y) Save on the R:\ drive Name file after a city outside of Texas 4/5/2004 ENGR 111A - Spring 2004

SOLUTION x = 0; while x <= 100 y = sin(x); x = x+5; disp(y) end 4/5/2004 ENGR 111A - Spring 2004

NESTED while LOOPS while loops can also be nested. Each loop must be closed with an end. break statements may be used to jump out of loops. 4/5/2004 ENGR 111A - Spring 2004

EXAMPLE r = 1; N = 10; while r <= N c = 1; while c <= N A(r,c) = 1/(r + c -1); c = c + 1; end r = r + 1; end %- note use of r,c instead of i,j. 4/5/2004 ENGR 111A - Spring 2004

Assignment 11.1 Individual assignment. Due: Nov 15, 2004 Palm MATLAB; Chapter 4: #22 (use for-loop in part b), 28 (use a while-loop), T4.5-4 (p. 225) Extra Problem (see assignments web page)! Extra Credit (see assignments web page)!! Read Palm section 4.7. 4/5/2004 ENGR 111A - Spring 2004