Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression.

Slides:



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

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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 5: Control Structures II (Repetition)
Chapter 8 Branching Statements and Program Design.
EGR 106 – Project Intro / While Loop Project description Project schedule Background information / terminology A simple example Examples of similar programs.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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.,
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
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.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Working with Loops, Conditional Statements, and Arrays.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
1 Structured Programming EEN170 Programming in MATLAB.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Beginning Programming for Engineers Matlab Conditional Computation.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lab 4: Loops and Iteration Graham Northup
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Control Statements.
Week 8 - Programming II Today – more features: Loop control
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection)
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 5: Control Structures II (Repetition)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > = <= == ~= Logical & | If operations have equal precedence the expression is executed from left to right What is the value of -2 > 5 > 1 ?

Week 9 - Programming II Today: –Control flow options beyond if/else –Loop options beyond for –Nesting Debugging tools Textbook chapter 7, pages , (sections 7.2.3, 7.3, 7.4.2, 7.5, 7.6 )

Extensions of if/else As introduced, if/else allows for two choices: if expression {commands if expression is true } else {commands if false } end

What if there are more than 2 situations? 3 situations: for a variable x, display: x = 0 x > 0 x < 0 4 situations: convert a compass angle to a direction: 0º  east 90º  north 180º  west 270º  south

Could use “nested” if/else commands

or

The “elseif” command if expression1 {commands if expression1 is true } elseif expression2 {commands if expression2 is true } else {commands if both are false } end

Examples: Note – many elseifs are allowed, but only 1 “else”

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

“Switch/Case” Use the value of a single variable or expression to determine what commands to execute: switch expression case value1 {command set 1} case value2 {command set 2} case value3 {command set 3} otherwise {command set 4} end

Example – convert compass angle to direction: note combining of possible values in braces note use of otherwise

Other control flow options: continue – jumps to next loop iteration: e.g. for k = 1:25000 if x(k) > 0 continue end { more commands } end return – terminates current function or script skip ahead

Longer Running Loops for loops repeat a fixed number of times: for variable = {array of length n} {commands} end and break can be used to stop earlier. Question: How about repeating “until done”?

Answer: Matlab’s “while” loop: while expression {commands to be repeated while expression is true} end

Example 1 – compounded interest until the amount doubles: value = 1000; for year = 1:1000 value = value * 1.08; fprintf('%2d years: %6.2f \n',year,value) if value > 2000 break end for loop terminated with break

Expected output:

while version value = 1000; year = 0; while value < 2000 value = value * 1.08; year = year + 1; fprintf('%2d years: %6.2f \n',year,value) end note the counter variable year

Example 3 – keeping time: time = input('how long to wait? '); while time > 0 disp([ num2str(time),' seconds left']) pause(1) time = time – 1; end disp('done') unnecessary relational op yet another counter variable

Example 4 – Collecting and storing data until a zero is entered: x = [ ]; new = 1; while new ~= 0 new = input('enter value '); x = [ x, new ]; end x = x(1:end–1) empty array to drop the zero initialize

Example 5 – Getting valid keyboard input: E.g. forcing the input to be between 0 and 10: x = -3; while ( x 10 ) x = input( 'type a value ' ); end

or: x = input('enter value '); while (x 10) disp('invalid choice'); x = input('enter value '); end disp('finally!');

Put These Together – Hi-Lo: numb = round (10*rand(1)); done = 0; while ~done guess = input('guess'); if guess = = numb disp( 'You got it !!!' ); done = 1; elseif guess > numb disp('too high') else disp('too low') end initialization single guess loop control

Loops within Loops – Nesting while expression1 {commands} while expression2 {commands} end {commands} end for index1 = array1 {commands} for index2 = array2 {commands} end {commands} end can be more than 2 levels deep

Example – computing a table of z = x 2 +y 2 for x and y equal to the integers 1, 2,…6: for x = 1:6 for y = 1:6 z(x,y) = x^2+y^2; end z

Debugging Tools so far, Run

Debugging ≡ finding and correcting errors (bugs) in programs Useful debugging tools: –Ability to stop a program in the middle of its execution (at a breakpoint) –Ability to examine variable values at that point –Ability to modify variable values at that point

controls for creating and removing breakpoints

indicator of breakpoint location (can have multiple breakpoints)

What shows up at the breakpoint Command window:Editor window: location indicator different prompt

Can single step (F10) or continue (F5) to the next breakpoint (or end)