Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.

Slides:



Advertisements
Similar presentations
How SAS implements structured programming constructs
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 4. Loops and Character Manipulation Loops in FORTRAN are constructs that permits us to execute a sequence of statements more than once. Type of.
Repeating Actions While and For Loops
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 5: Control Structures II (Repetition)
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Loops CS 103 February 19, 2007 Presented by Nate.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Chapter 3. Control Structures and Program Design ► Two broad categories of control statement:  Branches  Loops ► These will make the program more complex.
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:
Loops CS 103 February 13, 2009 Author: Nate Hamm.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 8 Loops. For Loop >Executes a block of statements a specified number of times. Form: for loop variable=first:incr:last statements end.
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.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
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.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
CSE123 - Lecture 4 Structured Programming- Loops.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING 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.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
ECE Application Programming
Lecture 7: Repeating a Known Number of Times
PROGRAM CONTROL STRUCTURE
CS1371 Introduction to Computing for Engineers
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Ch 7: JavaScript Control Statements I.
Programming Fundamentals
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Looping.
Lecture 4 MATLAB programming (2)
Arrays, For loop While loop Do while loop
Lecture 5 MATLAB programming (3)
Types of Flow of Control
Computing Fundamentals
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.
For loops Taken from notes by Dr. Neil Moore
EPSII 59:006 Spring 2004.
ECE 103 Engineering Programming Chapter 18 Iteration
Matlab Basics.
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
REPETITION Why Repetition?
Presentation transcript:

Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while loops ii. for loops.

A while loop is a block of statements that are repeated indefinitely as long as some condition is satisfied. while expression Code block... End The controlling expression produces a logical value. If the expression is true, the code block will be executed, and then control will return to the while statement. If the expression is still true, the statements will be executed again. This process will be repeated until the expression becomes false. When control returns to the while statement and the expression is false, the program will execute the first statement after the end.

1.State the Problem Calculate the average and the standard deviation of a set of measurements, assuming that all of the measurements are either positive or zero and assuming that we do not know in advance how many measurements are included in the data set. A negative input value will mark the end of the set of measurements. 2.Define the inputs and outputs The inputs required by this program are an unknown number of positive or zero numbers. The outputs from this program are a printout of the mean and the standard deviation of the input data set. In addition, we will print out the number of data points input to the program 3.Top down Design Accumulate the input data Calculate the mean and standard deviation Write out the mean, standard deviation, and number of points

4. Pseudo code 1. Initialize n, sum_x, and sum_x2 to 0 2. Prompt user for first number 3. Read in first x 4. while x >= 0 5. n <- n sum_x <- sum_x + x 7. sum_x2 <- sum_x2 + x^2 8. Prompt user for next number 9. Read in next x 10.end 11. x_bar <- sum_x / n 12.std_dev <- sqrt((n*sum_x2 - sum_x^2) / (n*(n-1))) 13.Write out the mean value x_bar 14.Write out the standard deviation std_dev 15.Write out the number of input data points n

5. MATLAB Code % Initialize sums. n = 0; sum_x = 0; sum_x2 = 0; % Read in first value x = input('Enter first value: '); % While Loop to read input values. while x >= 0 % Accumulate sums. n = n + 1; sum_x = sum_x + x; sum_x2 = sum_x2 + x^2; % Read in next value x = input('Enter next value: '); end % Calculate the mean and standard deviation x_bar = sum_x / n; std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); % Tell user. fprintf('The mean of this data set is: %f\n', x_bar); fprintf('The standard deviation is: %f\n', std_dev); fprintf('The number of data points is: %f\n', n);

The for loop is a loop that executes a block of statements a specified number of times. The for loop has the form for index = expr Body... end Index is the loop variable (also known as the loop index) and expr is the loop control expression. The expression usually takes the form of a vector in shortcut notation first:incr:last. The statements between the for statement and the end statement are known as the body of the loop. They are executed repeatedly during each pass of the for loop.

for ii = 1:10 Statement 1... Statement n end The loop index ii will be 1 the first time,2 the second time, and so on. The loop index will be 10 on the last pass through the statements. When control is returned to the for statement after the tenth pass, there are no more columns in the control expression, so execution transfers to the first statement after the end statement. Note that the loop index ii is still set to 10 after the loop finishes executing.

for ii = 1:2:10 Statement 1... Statement n end The loop index ii will be 1 the first time, 3 the second time, and so on. The loop index will be 9 on the fifth and last pass through the statements. When control is returned to the for statement after the fifth pass, there are no more columns in the control expression, so execution transfers to the first statement after the end statement. Note that the loop index ii is still set to 9 after the loop finishes executing.

for ii = [5 9 7] Statement 1... Statement n end Loop will be executed three times with the loop index set to 5 the first time, 9 the second time, and 7 the final time. The loop index ii is still set to 7 after the loop finishes executing.

for ii = [1 2 3;4 5 6] Statement 1... Statement n End The loop index ii will be the column vector the first time [1; 4], the second time [2 ; 5], and the third time [3 ; 6]. The loop index ii is still set to [3 ; 6] after the loop finishes executing. This example illustrates the fact that a loop index can be a vector.

To illustrate the operation of a for loop, we will use a for loop to calculate the factorial function. The factorial function is defined as N! = 1 N = 0 N! = N * (N-1) * (N-2) *... * 3 * 2 * 1 N > 0 The MATLAB code to calculate N factorial for positive value of N would be n_factorial = 1 for ii = 1:n n_factorial = n_factorial * ii; end Suppose that we wish to calculate the value of 5!. If n is 5, the for loop control expression would be the row vector [ ]. This loop will be executed 5 times, with the variable ii taking on values of 1, 2, 3, 4, and 5 in the successive loops. The resulting value of n_factorial will be 1 x 2 x 3 x 4 x 5 = 120

% Initialize sums. sum_x = 0; sum_x2 = 0; % Get the number of points to input. n = input('Enter number of points: '); % Loop to read input values. for ii = 1:n % Read in next value x = input('Enter value: '); % Accumulate sums. sum_x = sum_x + x; sum_x2 = sum_x2 + x^2; end % Now calculate statistics. x_bar = sum_x / n; std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); % Tell user. fprintf('The mean of this data set is: %f\n', x_bar); fprintf('The standard deviation is: %f\n', std_dev); fprintf('The number of data points is: %f\n', n); end

Till this lecture is the syllabus for Midterm i.e. on 26 th October Loops will be continued after the exam……………