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

Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
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 > =
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
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
Python quick start guide
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.
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.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
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)
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
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)
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Week 9 - Programming III Today: – Another loop option – A programming example: tic-tac-toe Textbook chapter 7, pages , (sections 7.4.2,
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.
CSI 3125, Preliminaries, page 1 Control Statements.
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.
JavaScript, Sixth Edition
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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
REPETITION CONTROL STRUCTURE
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Chapter 3: Decisions and Loops
Scripts & Functions Scripts and functions are contained in .m-files
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Week 8 - Programming II Today – more features: Loop control
Chapter 4: Control Structures I (Selection)
Arithmetic operations, decisions and looping
Chapter 4: Control Structures I (Selection)
Chapter8: Statement-Level Control Structures April 9, 2019
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 8 - 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

Could use “nested” if/else commands

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”

“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

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