Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Visual C++ Programming: Concepts and Projects
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
1 Chapter 4 Simple Selections and Repetitions INTRODUCTION The majority of challenging and interesting algorithms necessitate the ability to make.
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
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.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
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.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Introduction to branching.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Week 8: Decisions Bryan Burlingame 21 October 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Control statements Mostafa Abdallah
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Decision Making and Branching
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Matlab Programming for Engineers
Computer Science 210 Computer Organization
Matlab Training Session 4: Control, Flow and Functions
Lecture 3- Decision Structures
ITM 352 Flow-Control: if and switch
Control Structures.
Introduction to MATLAB
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Logical Operations In Matlab.
Introduction to Programming Using Python PART 2
Selection Statements.
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Matlab Basics.
Presentation transcript:

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to: additional operators branches to control operations loops to repeat operations

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

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

Matlab has Logical Operators as Well A operator 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 logical (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 ~ ~A~A xor(A,B) A|BA|BA&BBA “truth table”“unary” operator

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 on commands

Example – 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 – output a warning if the variable x is negative (note that there is no “else” portion in this example): x = … { computed somehow }; if x < 0 disp( ' Warning: negative value ' ) end the else component is not required

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

Example – Write a script to put 2 numbers in numerical order:

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

Example – implement a count down timer (in seconds):