ENGR 1181 MATLAB 09: For Loops 2

Similar documents
Programming for Experimental Research. Flow Control

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CEMTool Tutorial. Control statements

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

ENGR 1181 MATLAB 02: Array Creation

Computer Programming: C++

Introduction. C provides two styles of flow control:

Computer Programming

ENGR 1181 MATLAB 05: Input and Output

REPETITIVE EXECUTION: LOOPS

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

CS 221 Lecture. Tuesday, 11 October 2011

Chapter 7: Programming in MATLAB

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Unit 3 Decision making, Looping and Arrays

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

ECE 102 Engineering Computation

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

Repetition Structures Chapter 9

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

MATLAB - Lecture # 4

To become familiar with array manipulation, searching, and sorting.

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Multiple-Subscripted Arrays

Chapter 4: Control structures. Repetition

LECTURE 5 Control Structures Part 2

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Array Creation ENGR 1187 MATLAB 2

SFU CMPT Topic: Control Statements

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 4: Control structures

Prepared by: Shraddha Modi

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

7 Control Structures, Logical Statements

ECE 202 LAB 3 ADVANCED MATLAB

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

2.2 (a) Statement, subroutine, procedure, function, parameter, loop

The Mathematics of Big Data

ENGR 1181 MATLAB 4: Array Operations

Array Creation ENGR 1181 MATLAB 2

Fundamentals of Computer Programming Using C

Programming in MATLAB Part 2

Introduction to C/C++ Lecture 3 - Program Flow Control

COMPUTER PROGRAMMING LOOPS

Array Accessing and Strings ENGR 1181 MATLAB 3

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Short Version of Matlab Manual

ENGR 1181 MATLAB 15: Functions 2

LOOPS. Repetition using the while statement

function [s p] = sumprod (f, g)

Chapter 4 C Program Control

MATLAB Introduction To Engineering for ECE Topics Covered: 1. Creating Script Files (.m files) 2. Using the Real Time Debugger

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Introduction to MATLAB

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Chapter 2. The Algorithmic Foundations of. Computer Science INVITATION TO. Computer Science. Tuesday, September 10, 13

Conditionals and Loops

Physics 326G Winter Class 6

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

2 T. x + 2 T. , T( x, y = 0) = T 1

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Chapter 5: Control Structures II (Repetition)

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Introduction to MATLAB

CHAPTER 12 LCD AND KEYBOARD INTERFACING

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

Introduction to Programming using C++

Welcome to Starting Out with Programming Logic and Design, Third Edition.

Fundamentals of Programming Session 9

OER on Loops in C Programming

Branches, Conditional Statements

Review for Programming Exam and Final May 4-9, Ribbon with icons for commands Quick access toolbar (more at lecture end)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

EE 216 Experiment 1. MATLAB Structure and Use

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Chapter 4. Flow of Control

COURSE OF STUDY UNIT PLANNING GUIDE COMPUTER SCIENCE 1 FOR: 5 CREDITS GRADE LEVEL: 9-12 FULL-YEAR COURSE PREPARED BY: SUSIE EISEN

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Lecture 10. Daily Puzzle

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

MATLAB MATLAB - Lecture # 8. Programming in MATLAB / Chapter 7. if end if-else end if-elseif-else end

Accelerating Information Technology Innovation

Transcription:

ENGR 1181 MATLAB 09: For Loops Learning Objectives 1. Use more complex ways of setting the loop index. Construct nested loops in the following situations: a. For use with two dimensional arrays b. For repeating a calculation numerous times. Use loops to repeat a code with conditional statements Topics Students will read Chapter 6.4.1 and 6.4.6 of the MATLAB book before coming to class. This preparation material is provided to supplement this reading. Students will learn a more advanced understanding of while loops by expanding on the information presented in the Basic For Loops class. This knowledge will be used understand creating and executing nested for loops, as well as additional commands. This material contains the following: 1. Key Definitions. Review of for- loops. Nested for loops 4. The break Command 5. The continue Command 1. Key Definitions Loop a command, or group of commands, can be executed multiple times, consecutively. Relational Operator compares two numbers by determining whether a comparison statement is true or false. Operators include: less than <, greater than >, less than or equal to <=, greater than or equal to >=, equal to ==, not equal to ~=. Logical Operator examines true/false statements and produces a result that is true (1) or false (0) according to the specific operator. Operators include AND (&), OR ( ), NOT (~). 1

Order of Precedence computers and calculators follow a hierarchy for the order in which operations are executed. Higher-precedence operations are executed before lower-precedence operations; when they are equal, they are executed from left to right. Conditional Statements a command that allows a program to make a decision of whether to execute a set of commands or skip those commands.. Review of for- loops for loops are used when the number of passes is known (or can be calculated) in advance. A variable is used to control the looping process. The general structure of a for loop is: Loop index variable The value of k in the first pass The increment in k after each pass for k = f : s : t......... The value of k in the last pass A group of MATLAB commands In the first pass, k = f, and MATLAB executes the commands between the for and the commands The computer goes back to the for command for the second pass. k obtains a new value equal to k = f+s, and the commands between the for and the are executed with the new value of k. The process repeats itself until the last pass where k = t. Example: k = 1: : 9 o There are five passes through the loop. The values of k are: 1 5 7 9

. Nested for- loops A for loop can be nested within another for loop. for k = 1 : for n = 1 : 5 commands Every time k is increased by 1 the nested loop loops five times with the value of n ranging from 1 through 5. Overall the commands will be executed 15 times with the values of:

k = 1 n = 1 k = 1 n = k = 1 n = k = 1 n = 4 k = 1 n = 5 k = n = 1 k = n = k = n = k = n = 4 k = n = 5 k = n = 1 k = n = k = n = k = n = 4 k = n = 5 4

m n i j matrix 1 1 1 m n i j matrix 1 1 1 7 5

m n i j matrix 1 1 1 7 7 Now that the first pass of i is complete (because j has completed the designed n= passes), the loop returns back to the outside loop begins the second pass of i. Within the i= pass, the nested loop will complete passes to meet the required n= passes. These passes produce a row 7 1 7. m n i j matrix 1 1 1 7 7 7 1 7 1 6

Remember that within this nested loop, the program will complete the full cycle of passes for the outside loop, work inwards as required to the inside loop, complete the full cycle of passes for the inside loop, work back out, and then repeat the outside loop as many times as necessary. In the above nested loop, if now m = and n =, the result is a x matrix: 1 7 7 7 1 7 7 7 1 If the input was changed to m = and n = 5, then the following x 5 matrix would be created: 1 7 7 7 7 7 1 7 7 7 7 7 1 7 7 7

4. The break Command When MATLAB encounters a break command within a loop, MATLAB jumps to the command of the loop MATLAB then continues to execute the commands that follow The break command is typically used within a conditional if statement to terminate the execution of a loop if some condition is satisfied. Example of nested loop with break command a = 0; for x=1:10 a = a + 1; if (a == 5) break disp (a) 1 4 5. The continue Command The continue command resembles the break command but it only stops the remainder of the current iteration, not all iterations MATLAB jumps to the statement and continues execution with the NEXT iteration of the loop The continue command typically occurs within a conditional statement (if ) Example of nested loop with continue command a = 0; for x=1:10 a = a + 1; if (a == 5) continue disp (a) 1 4 6 7 8 9 10 8