Ch.5. Loops. (a.k.a. repetition or iteration)

Size: px
Start display at page:

Download "Ch.5. Loops. (a.k.a. repetition or iteration)"

Transcription

1

2 Ch.5 Loops (a.k.a. repetition or iteration)

3 5.1 The FOR loop End of for loop End of function

4 5.1 The FOR loop What is the answer for 100?

5 QUIZ

6 Modify the code to calculate the factorial of N: N!

7 Modify the code to calculate the factorial of N: N!

8

9 Vectors and for loops go hand-in-hand

10 Vectors and for loops go hand-in-hand

11 Vectors and for loops go hand-in-hand Write a script that uses a loop to find the product of all values stored in a vector.

12 Vectors and for loops go hand-in-hand Write a script that uses a loop to find the product of all values stored in a vector.

13 Vectors and for loops go hand-in-hand Write a script that uses a loop to find the maximum value stored in a vector.

14 What does this script do?

15 Here s a better way to do it

16 Exercise 5 Prompt the user for an integer n and print I love this stuff! n times.

17 Straight from the horse s mouth Source:

18 Straight from the horse s mouth Source:

19 Source: QUIZ

20 QUIZ Calculate the sum of all prime numbers between 0 and 15, using a for loop. Source:

21 What does this code do? numvec = []; for iv = 1:n inputnum = input( Enter a number: ); numvec(iv) = inputnum;

22 What does this code do? numvec = []; for iv = 1:n inputnum = input( Enter a number: ); numvec(iv) = inputnum; Because of the repeated extension operations, this code is inefficient!

23 Code efficiency: PREALLOCATION This runs a lot faster!

24 Remember cumulative sums and products

25 What does this code do?

26 Use preallocation!

27

28 To do for next time: Read pp of text Solve in notebook: end-of-chapter 2, 3

29 Exercise 7

30 Exercise 8

31 QUIZ: preallocation Write your own function my_cumprod to emulate the cumprod function. The argument is a vector vec, already initialized.

32 5.2 Nested for loops

33 Nested for loops Modify the script to print a triangle : * ** *** **** *****

34 Nested for loops

35 Matrices and nested for loops go hand-in-hand Print a 10x10 multiplication table

36 Print a 10x10 multiplication table Yes, it s OK for functions to return matrices!

37 More practice: Also print row and column labels for the table!

38 Matrices and nested for loops go hand-in-hand Write a script that calculates the sum of all the elements in a matrix. Initialize the matrix with random integers in the range

39 Matrices and nested for loops go hand-in-hand Write a script that calculates the sum of all the elements in a matrix. Create a 50x50 matrix Initialize the matrix with random integers in the range M = randi([-10 10], 50, 50, 'int8');

40 What does this code do?

41

42 QUIZ: How can we obtain the sum of each row instead?

43 QUIZ: Modify this code to output the sum of each row instead

44 The while loop

45 Exercise 23

46 It is possible to use more complex conditions: while x >= 42 && x <100 while x >= 42 ~found

47 What does this code do? Programming concept: sentinel

48 Another example of a sentinel

49 What else does this program do?

50 To do for next time: Read pp of text Solve in notebook: end-of-chapter 17

51 QUIZ: Based on this example

52 More complex error checking

53 5.4 Vectorizing We ve used loops to manipulate vectors and matrices

54 5.4 Vectorizing and we ve seen that all the usual operations can also be performed by built-in MATLAB functions!

55 Vectorizing sums and products >> v = v * 3 >> v = v / 2 >> v3 = v1 + v2 >> M = M + 42 >> M3 = M2 M1 >> v2 = v1.* v2 >> M2 = M1.^2 >> mata = matb./3 Notice the dot for *, /, and ^ Achtung! Array operators operate term-by-term!

56 QUIZ: What is printed here? >> M = [2:3; 4:5] >> M1 = [1,2; 3,4] >> M.* M1 >> M.^ 2

57 QUIZ

58

59 mat = mat.* 2;?

60 Vectorizing loops w/if statements Idea: Use vectorized logical functions!

61 Example: Vectorize this code using the function find() Hint: find the index of the sentinel!

62 Vectorize this code from Ch.4 using the function find()

63 Examples of vectorized logical ops. >> v = [ ] >> isgr = vec > 5 isgr = >> sum(isgr) ans = 3 >> isgr + 5 ans = 3

64 Examples of vectorized logical ops. >> v = [ ] >> isgr = vec > 5 isgr = >> vec(isgr) ans =

65 Why doesn t this work? >> v = [ ] >> isgr = vec > 5 isgr = >> vec(isgr) ans = >> vec([ ]) How do we fix it?

66 Why doesn t this work? >> v = [ ] >> vec(logical([ ])) ans =

67 Other vectorized logical functions >> false(2) ans = >> true(1,5) ans =

68 Other vectorized logical functions >> v = [ ]; >> any(v) ans = 1 >> all(v) ans = 0

69 Other vectorized logical functions >> v = [ ]; >> v1 = [ ]; >> find(v<3) ans = >> find(v==3) ans = 4 find() returns the index/indices of the elements that satisfy the condition

70

71 Exercise 29

72 Exercise 30

73 Can we use find() with matrices? Yes, but find() uses linear indexing! >> M=[1 2 3;4 5 6;7:9]; >> find(m > 5) ans =????

74 Other vectorized logical functions >> v = [ ]; >> v1 = [ ]; >> find(v<3) ans = >> find(v==3) ans = 4 >> isequal(v,v1) ans = 1

75 Other vectorized logical functions >> v1 = [ ]; >> v2 = [ ]; >> v1 & v2 ans = >> v1 v2 ans =

76 >> v1 = [ ]; >> v2 = [ ]; Print: --the values where v1 and v2 are equal --the positions where v1 and v2 are equal --the numbers of elements that are equal

77 Homework for Ch.5 End-of-chapter: 18, 19, 21, 28, 32, 34 Due: Friday, Oct.10 before exam

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5)

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5) QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5) QUIZ Ch.3 Introduction to MATLAB programming 3.1 Algorithms

More information

Loop Statements and Vectorizing Code

Loop Statements and Vectorizing Code CHAPTER 5 Loop Statements and Vectorizing Code KEY TERMS looping statements counted loops conditional loops action vectorized code iterate loop or iterator variable echo printing running sum running product

More information

Array Accessing and Strings ENGR 1187 MATLAB 3

Array Accessing and Strings ENGR 1187 MATLAB 3 Array Accessing and Strings ENGR 1187 MATLAB 3 Today's Topics Array Addressing (indexing) Vector Addressing (indexing) Matrix Addressing (indexing) Today's Topics Array Addressing (indexing) Vector Addressing

More information

Array Accessing and Strings ENGR 1181 MATLAB 3

Array Accessing and Strings ENGR 1181 MATLAB 3 Array Accessing and Strings ENGR 1181 MATLAB 3 Array Accessing In The Real World Recall from the previously class that seismic data is important in structural design for civil engineers. Accessing data

More information

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include

Looping Subtasks. We will examine some basic algorithms that use the while and if constructs. These subtasks include 1 Programming in C Looping Subtasks We will examine some basic algorithms that use the while and if constructs. These subtasks include Reading unknown quantity of data Counting things Accumulating (summing)

More information

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018) Vectors and Matrices Chapter 2 Linguaggio Programmazione Matlab-Simulink (2017/2018) Matrices A matrix is used to store a set of values of the same type; every value is stored in an element MATLAB stands

More information

ENGG1811 Computing for Engineers Week 10 Matlab: Vectorization. (No loops, please!)

ENGG1811 Computing for Engineers Week 10 Matlab: Vectorization. (No loops, please!) ENGG1811 Computing for Engineers Week 10 Matlab: Vectorization. (No loops, please!) ENGG1811 UNSW, CRICOS Provider No: 00098G1 W10 slide 1 Vectorisation Matlab is designed to work with vectors and matrices

More information

Lesson 7. Reading and Writing a.k.a. Input and Output

Lesson 7. Reading and Writing a.k.a. Input and Output Lesson 7 Reading and Writing a.k.a. Input and Output Escape sequences for printf strings Source: http://en.wikipedia.org/wiki/escape_sequences_in_c Escape sequences for printf strings Why do we need escape

More information

Computer Vision. Matlab

Computer Vision. Matlab Computer Vision Matlab A good choice for vision program development because Easy to do very rapid prototyping Quick to learn, and good documentation A good library of image processing functions Excellent

More information

Introduction to Matlab. By: Hossein Hamooni Fall 2014

Introduction to Matlab. By: Hossein Hamooni Fall 2014 Introduction to Matlab By: Hossein Hamooni Fall 2014 Why Matlab? Data analytics task Large data processing Multi-platform, Multi Format data importing Graphing Modeling Lots of built-in functions for rapid

More information

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2 PHYS 210: Introduction to Computational Physics MATLAB Exercises 2 Using your text editor, and working within /matlab, create a file named ex2.m that contains Matlab commands to solve the exercises enumerated

More information

An Introduction to MATLAB

An Introduction to MATLAB An Introduction to MATLAB Day 1 Simon Mitchell Simon.Mitchell@ucla.edu High level language Programing language and development environment Built-in development tools Numerical manipulation Plotting of

More information

QUIZ: What value is stored in a after this

QUIZ: What value is stored in a after this QUIZ: What value is stored in a after this statement is executed? Why? a = 23/7; QUIZ evaluates to 16. Lesson 4 Statements, Expressions, Operators Statement = complete instruction that directs the computer

More information

7 Control Structures, Logical Statements

7 Control Structures, Logical Statements 7 Control Structures, Logical Statements 7.1 Logical Statements 1. Logical (true or false) statements comparing scalars or matrices can be evaluated in MATLAB. Two matrices of the same size may be compared,

More information

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

function [s p] = sumprod (f, g) Outline of the Lecture Introduction to M-function programming Matlab Programming Example Relational operators Logical Operators Matlab Flow control structures Introduction to M-function programming M-files:

More information

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #16 Loops: Matrix Using Nested for Loop In this section, we will use the, for loop to code of the matrix problem.

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

Chapter 4: Control structures. Repetition

Chapter 4: Control structures. Repetition Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

CSE/NEUBEH 528 Homework 0: Introduction to Matlab CSE/NEUBEH 528 Homework 0: Introduction to Matlab (Practice only: Do not turn in) Okay, let s begin! Open Matlab by double-clicking the Matlab icon (on MS Windows systems) or typing matlab at the prompt

More information

Chapter 4: Control structures

Chapter 4: Control structures Chapter 4: Control structures Repetition Loop Statements After reading and studying this Section, student should be able to Implement repetition control in a program using while statements. Implement repetition

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

Loops. by Ahmet Sacan

Loops. by Ahmet Sacan Loops by Ahmet Sacan looping statements, counted loops, conditional loops, action, iterate, loop or iterator variable, running sum, running product, factorial, preallocate, echo printing, nested loop,

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MATLAB sessions: Laboratory MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs

More information

APPM 2460: Week Three For, While and If s

APPM 2460: Week Three For, While and If s APPM 2460: Week Three For, While and If s 1 Introduction Today we will learn a little more about programming. This time we will learn how to use for loops, while loops and if statements. 2 The For Loop

More information

Data Structures: Cell Arrays and Structures. Chapter 8. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Data Structures: Cell Arrays and Structures. Chapter 8. Linguaggio Programmazione Matlab-Simulink (2017/2018) Data Structures: Cell Arrays and Structures Chapter 8 Linguaggio Programmazione Matlab-Simulink (2017/2018) Cell Arrays A cell array is a type of data structure that can store different types of values

More information

MATLAB PROGRAMMING LECTURES. By Sarah Hussein

MATLAB PROGRAMMING LECTURES. By Sarah Hussein MATLAB PROGRAMMING LECTURES By Sarah Hussein Lecture 1: Introduction to MATLAB 1.1Introduction MATLAB is a mathematical and graphical software package with numerical, graphical, and programming capabilities.

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Control Flow: Loop Statements

Control Flow: Loop Statements Control Flow: Loop Statements A loop repeatedly executes a of sub-statements, called the loop body. Python provides two kinds of loop statements: a for-loop and a while-loop. This exercise gives you practice

More information

Statistical Computing (36-350)

Statistical Computing (36-350) Statistical Computing (36-350) Lecture 1: Introduction to the course; Data Cosma Shalizi and Vincent Vu 29 August 2011 Why good statisticians learn how to program Independence: otherwise, you rely on someone

More information

A Look Back at Arithmetic Operators: the Increment and Decrement

A Look Back at Arithmetic Operators: the Increment and Decrement A Look Back at Arithmetic Operators: the Increment and Decrement Spring Semester 2016 Programming and Data Structure 27 Increment (++) and Decrement (--) Both of these are unary operators; they operate

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

RECURSION: n. SEE RECURSION 3

RECURSION: n. SEE RECURSION 3 RECURSION: n. SEE RECURSION 3 COMPUTER SCIENCE 61A June 30, 2015 1 Recursion A recursive function is a function that calls itself. Below is a recursive factorial function. def factorial(n): if n == 0 or

More information

ECE 102 Engineering Computation

ECE 102 Engineering Computation ECE 102 Engineering Computation Phillip Wong MATLAB Loops for while break / continue Loops A loop changes the execution flow in a program. What happens in a loop? For each iteration of the loop, statements

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

1) As a logical statement, is 1 considered true or false in MATLAB? Explain your answer.

1) As a logical statement, is 1 considered true or false in MATLAB? Explain your answer. ENGR 1181 Midterm 2+ Review Note: This practice material does not contain actual test questions or represent the format of the final. The first 20 questions should be completed WITHOUT using MATLAB. This

More information

CS302: Self Check Quiz 2

CS302: Self Check Quiz 2 CS302: Self Check Quiz 2 name: Part I True or False For these questions, is the statement true or false? Assume the statements are about the Java programming language. 1.) The result of an expression with

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.7 Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways! Exercise 3 / Ch.8 Given the following array, write code

More information

RECURSION, RECURSION, (TREE) RECURSION! 3

RECURSION, RECURSION, (TREE) RECURSION! 3 RECURSION, RECURSION, (TREE) RECURSION! 3 COMPUTER SCIENCE 61A September 18, 2013 A function is recursive if it calls itself. Below is recursive factorial function. def factorial(n): if n == 0 or n ==

More information

INTRODUCTION OF LOOPS

INTRODUCTION OF LOOPS INTRODUCTION OF LOOPS For Loop Example 1: based on an existing array L, create another array R with each element having the absolute value of the corresponding one in array L. Input: L = [11-25 32-2 0

More information

LECTURE 5 Control Structures Part 2

LECTURE 5 Control Structures Part 2 LECTURE 5 Control Structures Part 2 REPETITION STATEMENTS Repetition statements are called loops, and are used to repeat the same code multiple times in succession. The number of repetitions is based on

More information

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

while (condition) { body_statements; for (initialization; condition; update) { body_statements; ITEC 136 Business Programming Concepts Week 01, Part 01 Overview 1 Week 7 Overview Week 6 review Four parts to every loop Initialization Condition Body Update Pre-test loops: condition is evaluated before

More information

Declaring a 2D Array

Declaring a 2D Array Lecture 13 Declaring a 2D Array Model: type name[row_size ][ column_size] Example: int grades[10][20]; string students[10][20]; 2D Array data structure Say we have the following array: int grades[4][8];

More information

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

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times. EGR 111 Loops This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times. New MATLAB commands: for, while,, length 1. The For Loop Suppose we want print a statement

More information

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

RECURSION, RECURSION, (TREE) RECURSION! 2

RECURSION, RECURSION, (TREE) RECURSION! 2 RECURSION, RECURSION, (TREE) RECURSION! 2 COMPUTER SCIENCE 61A February 5, 2015 A function is recursive if it calls itself. Below is a recursive factorial function. def factorial(n): if n == 0 or n ==

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MATLAB sessions: Laboratory MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs

More information

Repetition Structures II

Repetition Structures II Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi Types of Control Structures Sequential All programs that we have written so far The statements inside a pair

More information

Computational Finance

Computational Finance Computational Finance Introduction to Matlab Marek Kolman Matlab program/programming language for technical computing particularly for numerical issues works on matrix/vector basis usually used for functional

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Array Creation ENGR 1181 MATLAB 2

Array Creation ENGR 1181 MATLAB 2 Array Creation ENGR 1181 MATLAB 2 Array Creation In The Real World Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data are critical to

More information

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1 CSE123 LECTURE 3-1 Program Design and Control Structures Repetitions (Loops) 1-1 The Essentials of Repetition Loop Group of instructions computer executes repeatedly while some condition remains true Counter-controlled

More information

Dynamics and Vibrations Mupad tutorial

Dynamics and Vibrations Mupad tutorial Dynamics and Vibrations Mupad tutorial School of Engineering Brown University ENGN40 will be using Matlab Live Scripts instead of Mupad. You can find information about Live Scripts in the ENGN40 MATLAB

More information

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged. QUIZ Lesson 4 Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged. QUIZ Lesson 4 Exercise 4: Write an if statement that

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures 4. Łódź 2018 Exercise Harmonic Sum - Type in the program code - Save it as harmonic.py - Run the script using IPython Wikipedia - This program uses the for loop, the range()

More information

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations MATLAB for Experimental Research Fall 2018 Vectors, Matrices, Matrix Operations Matlab is more than a calculator! The array is a fundamental form that MATLAB uses to store and manipulate data. An array

More information

CS110D: PROGRAMMING LANGUAGE I

CS110D: PROGRAMMING LANGUAGE I CS110D: PROGRAMMING LANGUAGE I Computer Science department Lecture 5&6: Loops Lecture Contents Why loops?? While loops for loops do while loops Nested control structures Motivation Suppose that you need

More information

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB MAT 75 Laboratory Matrix Computations and Programming in MATLAB In this laboratory session we will learn how to. Create and manipulate matrices and vectors.. Write simple programs in MATLAB NOTE: For your

More information

Chapter 4 Introduction to Control Statements

Chapter 4 Introduction to Control Statements Introduction to Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives 2 How do you use the increment and decrement operators? What are the standard math methods?

More information

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx 1 of 9 FreeMat Tutorial FreeMat is a general purpose matrix calculator. It allows you to enter matrices and then perform operations on them in the same way you would write the operations on paper. This

More information

Engineering Innovation Center MATLAB Basics

Engineering Innovation Center MATLAB Basics Engineering Innovation Center MATLAB Basics MATLAB Short for MATrix LABoratory Both a programming language and the interface Designed to deal with large sets of data and analysis Allows matrix manipulations,

More information

1) As a logical statement, is 1 considered true or false in MATLAB? Explain your answer.

1) As a logical statement, is 1 considered true or false in MATLAB? Explain your answer. ENGR 1181 Midterm 2+ Review Note: This practice material does not contain actual test questions or represent the format of the final. The first 20 questions should be completed WITHOUT using MATLAB. This

More information

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science For loops, nested loops and scopes Jordi Cortadella Department of Computer Science Outline For loops Scopes Nested loops Introduction to Programming Dept. CS, UPC 2 Calculate x y Algorithm: repeated multiplication

More information

Flow Control: Branches and loops

Flow Control: Branches and loops Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the

More information

MATLAB. Devon Cormack and James Staley

MATLAB. Devon Cormack and James Staley MATLAB Devon Cormack and James Staley MATrix LABoratory Originally developed in 1970s as a FORTRAN wrapper, later rewritten in C Designed for the purpose of high-level numerical computation, visualization,

More information

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB Fall 2014 MAT 375 Numerical Methods Introduction to Programming using MATLAB Some useful links 1 The MOST useful link: www.google.com 2 MathWorks Webcite: www.mathworks.com/help/matlab/ 3 Wikibooks on

More information

ENGR 1181 MATLAB 09: For Loops 2

ENGR 1181 MATLAB 09: For Loops 2 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

More information

CS227-Scientific Computing. Lecture 3-MATLAB Programming

CS227-Scientific Computing. Lecture 3-MATLAB Programming CS227-Scientific Computing Lecture 3-MATLAB Programming Contents of this lecture Relational operators The MATLAB while Function M-files vs script M-files The MATLAB for Logical Operators The MATLAB if

More information

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

CSCD 255 HW 2. No string (char arrays) or any other kinds of array variables are allowed

CSCD 255 HW 2. No string (char arrays) or any other kinds of array variables are allowed CSCD 255 HW 2 Design a program called cscd255hw2.c which reads in a strictly positive integer (1 or greater) from the user. The user will then be prompted with a menu of choices (this menu should be repetitively

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

n Group of statements that are executed repeatedly while some condition remains true

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x Outline For loops, nested loops and scopes For loops Scopes Jordi Cortadella Department of Computer Science Nested loops Calculate x y Algorithm: repeated multiplication x x x x y times y x i p=x i 4 3

More information

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB SECTION 2: PROGRAMMING WITH MATLAB MAE 4020/5020 Numerical Methods with MATLAB 2 Functions and M Files M Files 3 Script file so called due to.m filename extension Contains a series of MATLAB commands The

More information

Computer Programming I - Unit 5 Lecture page 1 of 14

Computer Programming I - Unit 5 Lecture page 1 of 14 page 1 of 14 I. The while Statement while, for, do Loops Note: Loop - a control structure that causes a sequence of statement(s) to be executed repeatedly. The while statement is one of three looping statements

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 10 February 5 th, 2016 Abstract types: sets Lecture notes: Chapter 10 What is the value of this expresssion? let f (x:bool) (y:int) : int = if x then

More information

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

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing SECTION 5: STRUCTURED PROGRAMMING IN MATLAB ENGR 112 Introduction to Engineering Computing 2 Conditional Statements if statements if else statements Logical and relational operators switch case statements

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 02 / 15 / 2016 Instructor: Michael Eckmann Questions? Comments? Loops to repeat code while loops for loops do while loops Today s Topics Logical operators Example

More information

Repetition, Looping CS101

Repetition, Looping CS101 Repetition, Looping CS101 Last time we looked at how to use if-then statements to control the flow of a program. In this section we will look at different ways to repeat blocks of statements. Such repetitions

More information

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks MATLAB Basics Stanley Liang, PhD York University Configure a MATLAB Package Get a MATLAB Student License on Matworks Visit MathWorks at https://www.mathworks.com/ It is recommended signing up with a student

More information

CEMTool Tutorial. Control statements

CEMTool Tutorial. Control statements CEMTool Tutorial Control statements Overview This tutorial is part of the CEMWARE series. Each tutorial in this series will teach you a specific topic of common applications by explaining theoretical concepts

More information

11.3 Function Prototypes

11.3 Function Prototypes 11.3 Function Prototypes A Function Prototype contains the function s return type, name and parameter list Writing the function prototype is declaring the function. float square (float x); In a function

More information

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop

QUIZ: loops. Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 (inclusive) using: for loop while loop do...while loop QUIZ: loops Write a program that prints the integers from -7 to 15 using: for

More information

CS1 Lecture 3 Jan. 22, 2018

CS1 Lecture 3 Jan. 22, 2018 CS1 Lecture 3 Jan. 22, 2018 Office hours for me and for TAs have been posted, locations will change check class website regularly First homework available, due Mon., 9:00am. Discussion sections tomorrow

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

More information

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018 CS17 Integrated Introduction to Computer Science Klein Contents Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018 1 Bookends (Practice) 2 2 Subsets 3 3 Subset Sum 4 4 k-subsets 5 5 k-subset Sum 5 Objectives

More information

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose: Statement Purpose: The purpose of this Lab. is to practically familiarize student with how to write the common code once and reuse it without rewriting it using the concept of Methods. Activity Outcomes:

More information

Computer Programming: C++

Computer Programming: C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming: C++ Experiment #7 Arrays Part II Passing Array to a Function

More information

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points) Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow

More information

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible) Name Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code necessary C++ statements to perform the action described not a complete program Program a complete C++ program

More information

Lecture 11: Logical Functions & Selection Structures CMPSC 200 Programming for Engineers with MATLAB

Lecture 11: Logical Functions & Selection Structures CMPSC 200 Programming for Engineers with MATLAB Lecture 11: Logical Functions & Selection Structures CMPSC 2 Programming for Engineers with MATLAB Brad Sottile Fall 214 Slide 2 of 3 Midterm 1 Details You must bring your PSU student ID card Scantron

More information

In this lab, you will learn more about selection statements. You will get familiar to

In this lab, you will learn more about selection statements. You will get familiar to Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write

More information

Programming for Experimental Research. Flow Control

Programming for Experimental Research. Flow Control Programming for Experimental Research Flow Control FLOW CONTROL In a simple program, the commands are executed one after the other in the order they are typed. Many situations require more sophisticated

More information

Introudction to Computation and Cognition 2015 Dr. Oren Shriki Shay Ben-Sasson, Ofer Groweiss. MATLAB tutorial

Introudction to Computation and Cognition 2015 Dr. Oren Shriki Shay Ben-Sasson, Ofer Groweiss. MATLAB tutorial Introudction to Computation and Cognition 2015 Dr. Oren Shriki Shay Ben-Sasson, Ofer Groweiss MATLAB tutorial 1 MATLAB is a general-purpose mathematical software that is user friendly and very useful for

More information

Matlab- Command Window Operations, Scalars and Arrays

Matlab- Command Window Operations, Scalars and Arrays 1 ME313 Homework #1 Matlab- Command Window Operations, Scalars and Arrays Last Updated August 17 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents

More information

Lecture 15 MATLAB II: Conditional Statements and Arrays

Lecture 15 MATLAB II: Conditional Statements and Arrays Lecture 15 MATLAB II: Conditional Statements and Arrays 1 Conditional Statements 2 The boolean operators in MATLAB are: > greater than < less than >= greater than or equals

More information

Lab 9 Loops, Debugging

Lab 9 Loops, Debugging Lab 9 Loops, Debugging The following exercises are to be completed during lab class. If you do not have time to finish during lab, they must be completed before the beginning of the following lab session.

More information