Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Fall 2006AE6382 Design Computing1 Relational and Logical Operators Use relational operators to test two values Work with values of true and false Compare.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
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.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Beginning Programming for Engineers Matlab Conditional Computation.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Matlab Programming for Engineers
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
EGR 2261 Unit 4 Control Structures I: Selection
JavaScript: Control Statements.
Week 8 - Programming II Today – more features: Loop control
Chapter 8 JavaScript: Control Statements, Part 2
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Loop Statements & Vectorizing Code
INTRODUCTION TO MATLAB
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.
Chapter 5: Control Structures II (Repetition)
Matlab Basics.
Loop Statements & Vectorizing Code
Presentation transcript:

Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end

Week 7 - Programming I Introduce programming: –“scripts” and “functions” are programs of sequentially evaluated commands Today, extend features to: additional operators branches to control operations loops to repeat operations Textbook chapter 7, (sections 7.1, 7.2.1, 7.2.2, 7.4.1, 7.6)

Relational Operators Used to compare A and B: A op B A and B can be: –Variables or constants or expressions to compute –Scalars or arrays (watch the sizes on arrays!) –Numeric or string Result is true (1) or false (0) – perhaps an array Operators: >> == = << =~ =

Examples: expressionresult 5 < 71 [ ] > = [ ]1 1 0 max( 1:6 ) < = 7 1 [3 pi -12 ] > 'Tom' = = 'Bob'0 1 0 'Tom' = = 'm'0 0 1 Note – arrays and strings need to be the same size

Notes: Can compute using the result: e.g. –“how many of a particular letter in a state name?

Don’t confuse = = and = Round off errors can impact ~ = sin(0) = = 01 sin(pi) = = 00 instead, test size abs(sin(pi)) < = eps 1

Logical Operators Typically, used to combine A and B: A op B A and B can be: –Variables or constants or expressions to compute –Scalars or arrays, numeric or string A and B are interpreted as binary: –Numeric 0 is interpreted as false –All else is interpreted as true (equal to 1) Result is true (1) or false (0) – perhaps an array

Basic operators: and & or | xor not ~ ABA&BA|BA|Bxor(A,B)~A~A “truth table”“unary” operator

Examples: expressionresult (5 9)1 'Tom'= ='m' | 'Tom'= ='o' Also consider: name = input('enter name','s'); name = = 'Tom' | name = = 'Bob' or roll = sum(ceil(6*rand(1,2))); roll = = 7 | roll = = 11

Operator Precedence (left to right) 1.Parentheses ( ) 2.Transpose(') and power(.^) 3.Negation (-) and logical negation (~) 4.Multiplication (.*) and division (./), 5.Addition (+) and subtraction (-) 6.Colon operator (:) 7.Relational operators (, >=, = =, ~=) 8.Logical AND (&) 9.Logical OR (|)

Branches Conditional Statements Commands to select and execute certain blocks of code, skipping other blocks. Three types in Matlab: –if/else –switch –try/catch this week

“If/Else” Use relational and logical operators to determine what commands to execute: if expression {commands if true } else {commands if false } end evaluate this use of blue in editor; also, auto indentation

Example 1 – output whether a variable x is positive or not: x = … { computed somehow }; if x > 0 disp('the value is positive') else disp('the value is negative or zero') end

Example 2 – output a warning if the variable x is negative (note that there is no “else” portion in this test): x = … { computed somehow }; if x < 0 disp(‘Warning: negative value’) end no else component

Example 3 – ask if a plot should be drawn: x = input(‘Plot now? ‘,’s’); if x = = ‘yes’ | x = = ‘YES’ plot( ….. ) end more complicated expression

Loops Commands to repeatedly execute certain blocks of code Two types in Matlab: –for –while this week

The “for” Loop Used for a specific number of repetitions of a group of commands: for index = array { commands to be repeated go here } end Rules: One repetition per column of array index takes on the corresponding column’s values

Homework revisited – collect 7 digits of a telephone number and store in an array: for digit = 1:7 number(digit) = input('enter value '); end 7 repetitions since the array is [ ] digit cycles through the 7 values to create the 1 by 7 array “number”

Example 1 – calculating interest for 10 years: value = 1000; for year = 1:10 value = value * 1.08; fprintf('$ %6.2f after %2d years\n', value,year) end no need for a counter variable! year takes on the values 1 through 10

Example 2 – implement a count down timer (in seconds): time = input(‘how long? ‘); for count = time:-1:1 pause(1) disp([ num2str(count),’ seconds left’]) end disp(‘done’)

Example 3 – a general vector for array: for x = [ ] disp([ ' dial ', num2str(x) ]) pause(1) end

Example 4 – a matrix for array: board =(2*(rand(3,3)>.2)–1).*(rand(3,3)>.5) for x = board x end

Example 5 – even a string array: for x = 'EGR106' disp(x) end

Early Termination of Loops Use the break command: for variable = {array of length n} ….. break end Terminates the loop

Example – Hi-Lo: a guessing game with feedback select hidden number input guess correct? yes no provide hi/lo feedback 5 tries? yes no win lose

% HI-LO numb = round ( 100 * rand (1) ); for count = 1:5 guess = input('guess my number '); if guess = = numb disp( 'You got it !!!' ) break end if guess > numb disp('you are too high') else disp('you are too low') end if count = = 5 disp('Sorry, you lose! ') end

Example – calculating interest until the amount doubles using a for loop: value = 1000; for year = 1:1000 value = value * 1.08; fprintf('$ %6.2f after %2d years\n', value,year) if value >= 2000 break end will calculate up to 1000 years, if necessary if condition decides when to terminate loop