H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block

Size: px
Start display at page:

Download "H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block"

Transcription

1 Chapter 4 Branching Statements and Program Design Programming 1: Logical Operators, Logical Functions, and the IF-block Learning objectives: 1. Write simple program modules to implement single numerical methods and algorithms 2. Use variables, operators, and control structure to implement simple sequential algorithms 3. Use MATLAB m-file to create user-defined programs Topics/Outline: 1. Logical variables 2. Relational and logic operators 3. Logical functions 4. IF-block 5. Debugging Chapter 4: Branching Statements 1

2 Structured Programming Modular Design Subroutines (function M-files) called by a main program Top-Down Design a systematic development process that begins with the most general statement of a program s objective and then successively divides it into more detailed segments Structured Programming deals with how the actual code is developed so that it is easy to understand, correct, and modify Top-Down Design 1. Clearly state the problem that you are trying to solve 2. Define the inputs required by the program and the outputs to be produced by the program 3. Design the algorithm that you int to implement in the program Decomposition Stepwise refinement 4. Turn the algorithm into MATLAB statements 5. Test the resulting MATLAB program Chapter 4: Branching Statements 2

3 Algorithm Design The sequence of logical steps required to perform a specific task (solve a problem) Each step must be deterministic The process must always after a finite number of steps An algorithm cannot be open-ed The algorithm must be general enough to deal with any contingency Common Program Structures Sequence Selection Repetition Chapter 4: Branching Statements 3

4 Structured Programming Sequential paths Sequence all instructions (statements) are executed sequentially from top to bottom * A strict sequence is highly limiting Non-sequential paths Decisions (Selection) if, else, elseif Loops (Repetition) for, while, break Logical Data Type A special type of data that can have one of only two special possible values o true or false (when displayed in workspace) o 1 or 0 (when displayed in command window) It is legal to mix logical and numerical values in MATLAB (automatic conversion): 1 = true (and so as any non-zero numbers) 0 = false One program can handle multiple cases help computer make decisions Chapter 4: Branching Statements 4

5 Relational Operators Compare values stored in variables and return logical result MATLAB == ~= < <= > >= Interpretation is equal to is not equal to is less than is less than or equal to is greater than is greater than or equal to Note: Do not confuse the equivalence relational operator (==) with the assignment operator (=) Logic Operators Compare logical values and return logical result MATLAB & && xor ~ Interpretation Logical AND Logical AND with shortcut Logical inclusive OR Logical inclusive OR with shortcut Logical exclusive OR Logical NOT Binary logical operation : l 1 op l 2 Unary logical operation : op l 1 Chapter 4: Branching Statements 5

6 Truth Tables for Logic Operators Inputs and or xor not l 1 l 2 l 1 & l 2 l 1 &&l 2 l 1 l 2 l 1 l 2 xor(l 1,l 2 ) ~l 1 false false false false false false false true false true false false true true true true true false false false true true true false true true true true true true false false && and support short-circuit evaluations (partial evaluations), but only works between scalar values && will evaluate expression l 1 and immediately return a false value if l 1 is false will evaluate expression l 1 and immediately return a true value if l 1 is true xor is true if and only if one operand is true and the other one is false Partial Evaluations Most of the time, & and && produce the same result Partial evaluation is faster if the first operand is false Sometimes it is important to use shortcut expression Example: Evaluate whether cos(2a)/sin(b) is greater than 5 x = cos(2*a)/sin(b) > 5 To avoid the possibility of dividing by zero: x = (sin(b) ~= 0) && (cos(2*a)/sin(b)) > 5 Chapter 4: Branching Statements 6

7 Hierarchy of Operations 1. All arithmetic operations are evaluated first 2. All relational operators (==, ~=, >, >=, <, <=) are evaluated, from left to right 3. All ~ operators are evaluated, from left to right 4. All & and && operators are evaluated, from left to right 5. All,, and xor operators are evaluated, from left to right Relational Operators >> x=5; y=3; >> z1 = x >= y z1 = 1 (return 1 for true) >> z2 = (x - 2*y) == 3 z2 = 0 (return 0 for false) >> z3 = sqrt(x) ~= y^2 z3 = 1 (return 1 for true) >> z4 = 'A' < 'C' z4 = 1 (characters are evaluated in alphabetical order) Chapter 4: Branching Statements 7

8 Relational Operators >> a=[ ]; b = 0; >> c1 = a > b c1 = [ true false false true ] >> a=[3 0; -2 5]; b = 0; >> c2 = a < b c2 = >> a=[2 0; -2 5]; b=[5 1; -4 6]; >> c3 = a < b c3 = false true true false false false true true a a a ~ a a Logic Operators 0-1 matrix 0: false ; 1: True b c b ans b ans b & b c ans b b c ans Chapter 4: Branching Statements 8

9 Logic Operators >> value1 = true; value2 = false; value3 = 1; value4 = 0; >> value5 = 10; value6 = -5; value7 = [3 2; 0-1]; >> ~value1 ans = 0 >> ~value3 (convert 1 to true ) ans = 0 >> value1 value2 ans = 1 >> value6 & value7 (convert -5, 3, 2, -1 to true ans = 1 1 and 0 to false) 0 1 >> value5 && value7??? Operands to the and && operators must be convertible to logical scalar values. >> value1 + value5 ans = 11 Logic Functions Ischar(a) Return true if a is a character array and false otherwise Isempty(a) Return true if a is an empty array and false otherwise Isinf(a) Return true if a is infinite (Inf) and false otherwise Isnan(a) Return true if a is NaN (not a number) and false otherwise Isnumeric(a) Return true if a is a numeric array and false otherwise logical(a) Converts numerical values to logical values: if a is non-zero, it is converted to true. If a = 0, it is converted to false Chapter 4: Branching Statements 9

10 Logical Functions >> choice = input('continue? (y/n): ','s'); continue? (y/n): y >> strcmp(choice,'y') ans = 1 >> strcmp(choice,'n') ans = 0 strcmp: string comparison. Returns true(1) if choice stores y. Returns false(0)if choice stores n. Branches Branches are MATLAB statements that permit us to select and execute specific sections (blocks) of code while skipping other sections. if switch try/catch Chapter 4: Branching Statements 10

11 Selection (IF) Statements The most common form of selection structure is simple if statement The if statement will have a condition associated with it The condition is typically a logical expression that must be evaluated as either true or false The outcome of the evaluation will determine the next step performed Logical IF Statements If (condition) executable_statements if (x <= -1.0 x >= 1.0) y = 0. if (x > -1.0 & x < 0.) y = 1. + x if (x >= 0. & x < 1.0) y = 1.- x y x Chapter 4: Branching Statements 11

12 Nested IF Statement Structures can be nested within each other if (condition) statement block elseif (condition) another statement block else another statement block Can have any number of elseif, but at most one else How to use Nested IF If the condition is true the statements following the statement block are executed. If the condition is not true, then the control is transferred to the next else, elseif, or statement at the same if level. There can be any number of elseif clauses (0 or more) in an if construct, but there can be at most one else clause. Chapter 4: Branching Statements 12

13 else and elseif if temperature > 100 disp( Too hot - equipment malfunctioning. ) elseif temperature > 75 disp( Normal operating range. ) elseif temperature > 60 disp( Temperature below desired operating range. ) else disp( Too Cold - turn off equipment. ) Nested IF Statements Nested if (if, if else, if elseif) if (x <= -1.0) y = 0. elseif (x <= 0.) y = 1. + x elseif (x <= 1.0) y = 1. - x else y=0. y x Chapter 4: Branching Statements 13

14 Bungee Jumper Bungee chord length = L Initial height of bungee jumper = H > L Water depth = h if y <= L solve with free-fall equation elseif (y > L) & (y <= H) solve with spring tension elseif (y > H) & (y < H+h) solve with spring tension and water resistance else disp('hit Lake Bottom! Ouch!') Only the first true block will execute elseif, else are optional Always indent ( for clarity, does not change result) Chapter 4: Branching Statements 14

15 Nested IF Statements Nesting: the block inside one IF block may have additional IF blocks if (condition) statement block if (condition) another statement block else another statement block else another statement block keep indenting each block needs its own Nesting and Indentation Example: Roots of a Quadratic Equation x ax 2 bx c 0 2 b b 4ac 2a If a=0, b=0, no solution (or trivial sol. c=0) If a=0, b 0, one real root: x=-c/b If a 0, d=b 2 4ac 0, two real roots If a 0, d=b 2 4ac <0, two complex roots Chapter 4: Branching Statements 15

16 Nesting and Indentation function quadroots(a,b,c) % Computes real and complex roots of quadratic equation % a*x^2 + b*x + c = 0 % Output: (r1,i1,r2,i2) - real and imaginary parts of the % first and second root if a == 0 % weird cases if b ~= 0 % single root r1 = -c/b else % trivial solution error('trivial or No Solution. Try again') % quadratic formula else d = b^2-4*a*c; % discriminant if d >= 0 % real roots r1 = (-b + sqrt(d)) / (2*a) r2 = (-b - sqrt(d)) / (2*a) else % complex roots r1 = -b / (2*a) r2 = r1 i1 = sqrt(abs(d)) / (2*a) i2 = -i1 Roots of Quadratic Equation >> quadroots(5,3,-4) r1 = r2 = (two real roots) >> quad = quadroots(5,3,4) r1 = r2 = i1 = i2 = (two complex roots) >> quadroots(0,0,5)??? Error using ==> quadroots Trivial or No Solution. Try again (no root) Chapter 4: Branching Statements 16

17 Switch Structure Similar to if elseif, but the branching is based on the value of a single test expression switch testexpression case value statement block case value another statement block otherwise another statement block Switch Example: Determine the number of days in a month (Apr, Jun, Sept, Nov.) (February) Chapter 4: Branching Statements 17

18 Switch Example: Determine the number of days in a month >> ndays_in_month Enter the year = 2011 Enter the month (1-12) = 2 number of days in this month = 28 >> ndays_in_month Enter the year = 3680 Enter the month (1-12) = 2 number of days in this month = 29 >> ndays_in_month Enter the year = 2500 Enter the month (1-12) = 10 number of days in this month = 31 Try/Catch Statements A special branching construct designed to trap errors when debugging the MATLAB code try statement block catch another statement block 1. Execute the statements in the try block first 2. If no error occurs, skip the catch block and continue to the first statement following the 3. If an error does occur in the try block, the program will immediately execute the statements in catch block Chapter 4: Branching Statements 18

19 The try/catch Construct Using trap/catch in debugging to trap error Execute the catch block only if error is detected in the try block Trap error using trap/catch construct >> try_catch Enter array A = [ ]; Enter index of element to display: 8 A(8) = -3 >> try_catch Enter array A = [ ]; Enter index of element to display: 12 Illegal array index: 12 Exceeding matrix dimension: size(a) = 10 >> try_catch Enter array A = 0:0.1:10; Enter index of element to display: 95 A(95) = 9.4 >> try_catch Enter array A = 0:0.1:10; Enter index of element to display: 200 Illegal array index: 200 Exceeding matrix dimension: size(a) = 101 Chapter 4: Branching Statements 19

Lecture 3 MATLAB programming (1) Dr.Qi Ying

Lecture 3 MATLAB programming (1) Dr.Qi Ying Lecture 3 MATLAB programming (1) Dr.Qi Ying Objectives Data types Logical operators/functions Branching Debugging of a program Data types in MATLAB Basic: Numeric (integer, floating-point, complex) Logical:

More information

Files and File Management Scripts Logical Operations Conditional Statements

Files and File Management Scripts Logical Operations Conditional Statements Files and File Management Scripts Logical Operations Conditional Statements Files and File Management Matlab provides a group of commands to manage user files pwd: Print working directory displays the

More information

Chapter 4 Branching Statements & Program Design

Chapter 4 Branching Statements & Program Design EGR115 Introduction to Computing for Engineers Branching Statements & Program Design from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. 2016 Cengage Learning Topics Introduction: Program Design

More information

C/C++ Programming for Engineers: Matlab Branches and Loops

C/C++ Programming for Engineers: Matlab Branches and Loops C/C++ Programming for Engineers: Matlab Branches and Loops John T. Bell Department of Computer Science University of Illinois, Chicago Review What is the difference between a script and a function in Matlab?

More information

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28

Control Structures. March 1, Dr. Mihail. (Dr. Mihail) Control March 1, / 28 Control Structures Dr. Mihail March 1, 2015 (Dr. Mihail) Control March 1, 2015 1 / 28 Overview So far in this course, MATLAB programs consisted of a ordered sequence of mathematical operations, functions,

More information

Structure Array 1 / 50

Structure Array 1 / 50 Structure Array A structure array is a data type that groups related data using data containers called fields. Each field can contain any type of data. Access data in a structure using dot notation of

More information

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1

Lecture 9. Monday, January 31 CS 205 Programming for the Sciences - Lecture 9 1 Lecture 9 Reminder: Programming Assignment 3 is due Wednesday by 4:30pm. Exam 1 is on Friday. Exactly like Prog. Assign. 2; no collaboration or help from the instructor. Log into Windows/ACENET. Start

More information

Short Version of Matlab Manual

Short Version of Matlab Manual Short Version of Matlab Manual This is an extract from the manual which was used in MA10126 in first year. Its purpose is to refamiliarise you with the matlab programming concepts. 1 Starting MATLAB 1.1.1.

More information

Selection Statements

Selection Statements Selection Statements by Ahmet Sacan selection statements, branching statements, condition, relational expression, Boolean expression, logical expression, relational operators, logical operators, truth

More information

Branches, Conditional Statements

Branches, Conditional Statements Branches, Conditional Statements Branches, Conditional Statements A conditional statement lets you execute lines of code if some condition is met. There are 3 general forms in MATLAB: if if/else if/elseif/else

More information

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Flow of Control. Flow of control The order in which statements are executed. Transfer of control 1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control

More information

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays MATLAB MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering saksoy@cs.bilkent.edu.tr MATLAB Basics Top-down Program Design, Relational and Logical Operators Branches and Loops

More information

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering Computer Programming ECIV 2303 Chapter 6 Programming in MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering 1 Introduction A computer program is a sequence of computer

More information

PROGRAM DESIGN TOOLS. Algorithms, Flow Charts, Pseudo codes and Decision Tables. Designed by Parul Khurana, LIECA.

PROGRAM DESIGN TOOLS. Algorithms, Flow Charts, Pseudo codes and Decision Tables. Designed by Parul Khurana, LIECA. PROGRAM DESIGN TOOLS Algorithms, Flow Charts, Pseudo codes and Decision Tables Introduction The various tools collectively referred to as program design tools, that helps in planning the program are:-

More information

5. Selection: If and Switch Controls

5. Selection: If and Switch Controls Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

More information

Relational & Logical Operators, Selection Statements

Relational & Logical Operators, Selection Statements Relational & Logical Operators, Selection Statements by Ahmet Sacan selection statements, branching statements, condition, relational expression, Boolean expression, logical expression, relational operators,

More information

Dr. Khaled Al-Qawasmi

Dr. Khaled Al-Qawasmi Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351 Dr. Khaled Al-Qawasmi ١ Dr. Kahled Al-Qawasmi 2010-2011 Chapter 3 Selection Statements

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

PLD Semester Exam Study Guide Dec. 2018

PLD Semester Exam Study Guide Dec. 2018 Covers material from Chapters 1-8. Semester Exam will be built from these questions and answers, though they will be re-ordered and re-numbered and possibly worded slightly differently than on this study

More information

MATLAB Operators, control flow and scripting. Edited by Péter Vass

MATLAB Operators, control flow and scripting. Edited by Péter Vass MATLAB Operators, control flow and scripting Edited by Péter Vass Operators An operator is a symbol which is used for specifying some kind of operation to be executed. An operator is always the member

More information

Chapter 7: Programming in MATLAB

Chapter 7: Programming in MATLAB The Islamic University of Gaza Faculty of Engineering Civil Engineering Department Computer Programming (ECIV 2302) Chapter 7: Programming in MATLAB 1 7.1 Relational and Logical Operators == Equal to ~=

More information

Physics 326G Winter Class 6

Physics 326G Winter Class 6 Physics 36G Winter 008 Class 6 Today we will learn about functions, and also about some basic programming that allows you to control the execution of commands in the programs you write. You have already

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

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control COGS 119/219 MATLAB for Experimental Research Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control .m files We can write the MATLAB commands that we type at the command window

More information

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices Introduction to Interactive Calculations Matlab is interactive, no need to declare variables >> 2+3*4/2 >> V = 50 >> V + 2 >> V Ans = 52 >> a=5e-3; b=1; a+b Most elementary functions and constants are

More information

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Unit-II Programming and Problem Solving (BE1/4 CSE-2) Unit-II Programming and Problem Solving (BE1/4 CSE-2) Problem Solving: Algorithm: It is a part of the plan for the computer program. An algorithm is an effective procedure for solving a problem in a finite

More information

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

4.0 Programming with MATLAB

4.0 Programming with MATLAB 4.0 Programming with MATLAB 4.1 M-files The term M-file is obtained from the fact that such files are stored with.m extension. M-files are alternative means of performing computations so as to expand MATLAB

More information

Control Statements. If Statement if statement tests a particular condition

Control Statements. If Statement if statement tests a particular condition Control Statements Control Statements Define the way of flow in which the program statements should take place. Implement decisions and repetitions. There are four types of controls in C: Bi-directional

More information

Previous Lecture (and Discussion): Branching (if, elseif, else, end) Relational operators (<, >=, ==, ~=,, etc.) Logical operators (&&,, ~)

Previous Lecture (and Discussion): Branching (if, elseif, else, end) Relational operators (<, >=, ==, ~=,, etc.) Logical operators (&&,, ~) Previous Lecture (and Discussion): Branching (if, if,, ) Relational operators (=, ==, ~=,, etc.) Logical operators (&&,, ~) Today s Lecture: Logical operators and short-circuiting More branching nesting

More information

Relational and Logical Operators

Relational and Logical Operators Relational and Logical Operators Relational Operators Relational operators are used to represent conditions (such as space 0 in the water tank example) Result of the condition is either true or false In

More information

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program

Introduction. Like other programming languages, MATLAB has means for modifying the flow of a program Flow control 1 Introduction Like other programming languages, MATLAB has means for modifying the flow of a program All common constructs are implemented in MATLAB: for while if then else switch try 2 FOR

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

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

Selection Statements

Selection Statements Selection Statements 1 Introduction Matlab has two basic selection statements: the if statement and the switch statement. The if statement has optional else and elseif. The relational/comparison and logical

More information

Boolean Logic & Branching Lab Conditional Tests

Boolean Logic & Branching Lab Conditional Tests I. Boolean (Logical) Operations Boolean Logic & Branching Lab Conditional Tests 1. Review of Binary logic Three basic logical operations are commonly used in binary logic: and, or, and not. Table 1 lists

More information

Introduction to MATLAB

Introduction to MATLAB Introduction to MATLAB Dr./ Ahmed Nagib Mechanical Engineering department, Alexandria university, Egypt Spring 2017 Chapter 4 Decision making and looping functions (If, for and while functions) 4-1 Flowcharts

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2016 February 2, 2016 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2015 Chapter 7 Part 1 The Department of Computer Science Objectives 2 To understand the programming pattern simple decision and its implementation

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Fondamenti di Informatica

Fondamenti di Informatica Fondamenti di Informatica Execution control Prof. Emiliano Casalicchio emiliano.casalicchio@uniroma2.it Objectives This chapter discusses techniques for changing the flow of control of a program, which

More information

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Lecture 6 COP 3014 Spring February 11, 2018 Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its

More information

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence Expression Evaluation: Outline CSCI: 4500/6500 Programming Languages Control Flow Chapter 6 Infix, Prefix or Postfix Precedence and Associativity Side effects Statement versus Expression Oriented Languages

More information

Chapters 6-7. User-Defined Functions

Chapters 6-7. User-Defined Functions Chapters 6-7 User-Defined Functions User-Defined Functions, Iteration, and Debugging Strategies Learning objectives: 1. Write simple program modules to implement single numerical methods and algorithms

More information

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4. Introduction to Visual Basic and Visual C++ Arithmetic Expression Lesson 4 Calculation I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Arithmetic Expression Using Arithmetic Expression Calculations

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 4: Programming in Matlab Yasemin Bekiroglu (yaseminb@kth.se) Florian Pokorny(fpokorny@kth.se) Overview Overview Lecture 4: Programming in Matlab Wrap Up More on Scripts and Functions Wrap Up Last

More information

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement. 1. C#, C++, C, and Java use the symbol as the logical OR operator. a. $ b. % c. ^ d. 2. errors are relatively easy to locate and correct because the compiler or interpreter you use highlights every error.

More information

Unit 1 Quadratic Functions

Unit 1 Quadratic Functions Unit 1 Quadratic Functions This unit extends the study of quadratic functions to include in-depth analysis of general quadratic functions in both the standard form f ( x) = ax + bx + c and in the vertex

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up More on Scripts and Functions Basic Programming Lecture 2 Lecture 3 Lecture 4 Wrap Up Last time Loading data from file: load( filename ) Graphical input and

More information

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs

MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Lisa A. Oberbroeckling Loyola University Maryland loberbroeckling@loyola.edu L. Oberbroeckling (Loyola University) MATLAB 10/07/10

More information

Introduction to Computer Programming for Non-Majors

Introduction to Computer Programming for Non-Majors Introduction to Computer Programming for Non-Majors CSC 2301, Fall 2016 Chapter 7 Part 1 Instructor: Long Ma The Department of Computer Science Objectives---Decision Structures 2 To understand the programming

More information

COMP 208 Computers in Engineering

COMP 208 Computers in Engineering COMP 208 Computers in Engineering Lecture 14 Jun Wang School of Computer Science McGill University Fall 2007 COMP 208 - Lecture 14 1 Review: basics of C C is case sensitive 2 types of comments: /* */,

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

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation CS113: Lecture 3 Topics: Variables Data types Arithmetic and Bitwise Operators Order of Evaluation 1 Variables Names of variables: Composed of letters, digits, and the underscore ( ) character. (NO spaces;

More information

Selections. Zheng-Liang Lu 91 / 120

Selections. Zheng-Liang Lu 91 / 120 Selections ˆ Selection enables us to write programs that make decisions on. ˆ Selection structures contain one or more of the if, else, and elseif statements. ˆ The end statement denotes the end of selection

More information

Chapter 4. Flow of Control

Chapter 4. Flow of Control Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr Sequential flow of control

More information

Introduction to programming in MATLAB

Introduction to programming in MATLAB Master Degree Course in ELECTRONICS ENGINEERING http://www.dii.unimore.it/~lbiagiotti/systemscontroltheory.html Introduction to programming in MATLAB e-mail: luigi.biagiotti@unimore.it http://www.dii.unimore.it/~lbiagiotti

More information

Chapter 3: Programming with MATLAB

Chapter 3: Programming with MATLAB Chapter 3: Programming with MATLAB Choi Hae Jin Chapter Objectives q Learning how to create well-documented M-files in the edit window and invoke them from the command window. q Understanding how script

More information

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement Maltepe University Computer Engineering Department Algorithms and Programming Chapter 4: Conditionals - If statement - Switch statement Control Structures in C Control structures control the flow of execution

More information

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15 Table of Contents 1 INTRODUCTION... 1 2 IF... 1 2.1 BOOLEAN EXPRESSIONS... 3 2.2 BLOCKS... 3 2.3 IF-ELSE... 4 2.4 NESTING... 5 3 SWITCH (SOMETIMES KNOWN AS CASE )... 6 3.1 A BIT ABOUT BREAK... 7 4 CONDITIONAL

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment What is MATLAB? MATLAB PROGRAMMING Stands for MATrix LABoratory A software built around vectors and matrices A great tool for numerical computation of mathematical problems, such as Calculus Has powerful

More information

Programming for Engineers Iteration

Programming for Engineers Iteration Programming for Engineers Iteration ICEN 200 Spring 2018 Prof. Dola Saha 1 Data type conversions Grade average example,-./0 class average = 23450-67 893/0298 Grade and number of students can be integers

More information

Relational and Logical Statements

Relational and Logical Statements Relational and Logical Statements Relational Operators in MATLAB A operator B A and B can be: Variables or constants or expressions to compute Scalars or arrays Numeric or string Operators: > (greater

More information

Dr. Iyad Jafar. Adapted from the publisher slides

Dr. Iyad Jafar. Adapted from the publisher slides Computer Applications Lab Lab 5 Programming in Matlab Chapter 4 Sections 1,2,3,4 Dr. Iyad Jafar Adapted from the publisher slides Outline Program design and development Relational operators and logical

More information

Topics. Chapter 5. Equality Operators

Topics. Chapter 5. Equality Operators Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator

More information

Mini-Matlab Lesson 5: Functions and Loops

Mini-Matlab Lesson 5: Functions and Loops Mini-Matlab Lesson 5: Functions and Loops Writing Functions and Scripts Contents Relational and logical operators IF loops FOR loops WHILE loops Scripts and functions Defining and using functions Anonymous

More information

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14 Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to

More information

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999 Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999 1999 by CRC PRESS LLC CHAPTER THREE CONTROL STATEMENTS 3.1 FOR

More information

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) CP Lect 5 slide 1 Monday 2 October 2017 Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) Cristina Alexandru Monday 2 October 2017 Last Lecture Arithmetic Quadratic equation

More information

Chapter 4: Programming with MATLAB

Chapter 4: Programming with MATLAB Chapter 4: Programming with MATLAB Topics Covered: Programming Overview Relational Operators and Logical Variables Logical Operators and Functions Conditional Statements For Loops While Loops Debugging

More information

The Fortran Basics. Handout Two February 10, 2006

The Fortran Basics. Handout Two February 10, 2006 The Fortran Basics Handout Two February 10, 2006 A Fortran program consists of a sequential list of Fortran statements and constructs. A statement can be seen a continuous line of code, like b=a*a*a or

More information

Write a code fragment that prints yes if xc is in the interval and no if it is not.

Write a code fragment that prints yes if xc is in the interval and no if it is not. CS2 Lecture 4 28/9/9 Previous Lecture: Branching Logical operators oday s Lecture: Logical operators and values More branching nesting he idea of repetition Announcements: Section this week in the computer

More information

FONDAMENTI DI INFORMATICA. Prof. Emiliano Casalicchio

FONDAMENTI DI INFORMATICA. Prof. Emiliano Casalicchio FONDAMENTI DI INFORMATICA Prof. Emiliano Casalicchio casalicchio@ing.uniroma2.it 13/04/2015 Fondamenti di Informatica a.a. 2014/15 - E. Casalicchio 2 Objectives of this lesson We ll discuss Code blocks

More information

Chapter 2 REXX STATEMENTS. SYS-ED/ Computer Education Techniques, Inc.

Chapter 2 REXX STATEMENTS. SYS-ED/ Computer Education Techniques, Inc. Chapter 2 REXX STATEMENTS SYS-ED/ Computer Education Techniques, Inc. Objectives You will learn: Variables. REXX expressions. Concatenation. Conditional programming and flow of control. Condition traps.

More information

1.1 - Functions, Domain, and Range

1.1 - Functions, Domain, and Range 1.1 - Functions, Domain, and Range Lesson Outline Section 1: Difference between relations and functions Section 2: Use the vertical line test to check if it is a relation or a function Section 3: Domain

More information

Matlab Basics Lecture 2. Juha Kuortti October 28, 2017

Matlab Basics Lecture 2. Juha Kuortti October 28, 2017 Matlab Basics Lecture 2 Juha Kuortti October 28, 2017 1 Lecture 2 Logical operators Flow Control 2 Relational Operators Relational operators are used to compare variables. There are 6 comparison available:

More information

Selection Statements. Chapter 4. Copyright 2013 Elsevier Inc. All rights reserved 1

Selection Statements. Chapter 4. Copyright 2013 Elsevier Inc. All rights reserved 1 Selection Statements Chapter 4 Copyright 2013 Elsevier Inc. All rights reserved 1 Recall Relational Expressions The relational operators in MATLAB are: > greater than < less than >= greater than or equals

More information

Programming Logic and Design Ninth Edition

Programming Logic and Design Ninth Edition Programming Logic and Design Ninth Edition Chapter 3 Understanding Structure The Disadvantages of Unstructured Spaghetti Code Spaghetti code Logically snarled program statements Often a complicated mess

More information

What We Will Learn Today

What We Will Learn Today Lecture Notes 11-19-09 ENGR 0011 - Dr. Lund What we ve learned so far About the MATLAB environment Command Window Workspace Window Current Directory Window History Window How to enter calculations (and

More information

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D.

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D. Scilab Programming The open source platform for numerical computation Satish Annigeri Ph.D. Professor, Civil Engineering Department B.V.B. College of Engineering & Technology Hubli 580 031 satish@bvb.edu

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

Programming in MATLAB

Programming in MATLAB 8. Program Flow and Recursion Faculty of mathematics, physics and informatics Comenius University in Bratislava November 25th, 2015 Program Flow Program Flow in the first lecture: one command at a time

More information

Conditions, logical expressions, and selection. Introduction to control structures

Conditions, logical expressions, and selection. Introduction to control structures Conditions, logical expressions, and selection Introduction to control structures 1 Flow of control In a program, statements execute in a particular order By default, statements are executed sequentially:

More information

Binghamton University. CS-211 Fall Control Flow

Binghamton University. CS-211 Fall Control Flow Control Flow 1 Sequential Control Flow int x = 7; int y = 10; printf( x+y=%d\n,x+y); If Conditional Processing 3 Simple If statement syntax if (condition) then_statement; condition : Any expression whose

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops PRG PROGRAMMING ESSENTIALS 1 Lecture 2 Program flow, Conditionals, Loops https://cw.fel.cvut.cz/wiki/courses/be5b33prg/start Michal Reinštein Czech Technical University in Prague, Faculty of Electrical

More information

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15

MATLAB BASICS. Macro II. February 25, T.A.: Lucila Berniell. Macro II (PhD - uc3m) MatLab Basics 02/25/ / 15 MATLAB BASICS Macro II T.A.: Lucila Berniell February 25, 2010 Macro II (PhD - uc3m) MatLab Basics 02/25/2010 1 / 15 MATLAB windows Macro II (PhD - uc3m) MatLab Basics 02/25/2010 2 / 15 Numbers, vectors

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions

More information

Programming Logic and Design Sixth Edition

Programming Logic and Design Sixth Edition Objectives Programming Logic and Design Sixth Edition Chapter 4 Making Decisions In this chapter, you will learn about: Evaluating Boolean expressions to make comparisons The relational comparison operators

More information

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Copyright 2011 Pearson Addison-Wesley. All rights reserved.

More information

b. Suppose you enter input from the console, when you run the program. What is the output?

b. Suppose you enter input from the console, when you run the program. What is the output? Part I. Show the printout of the following code: (write the printout next to each println statement if the println statement is executed in the program). a. Show the output of the following code: public

More information

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases. Formulating Algorithms with Top-Down, Stepwise Refinement Case Study 2: Sentinel-Controlled Repetition In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability.

More information

SMS 3515: Scientific Computing. Sem /2015

SMS 3515: Scientific Computing. Sem /2015 s s SMS 3515: Scientific Computing Department of Computational and Theoretical Sciences, Kulliyyah of Science, International Islamic University Malaysia. Sem 1 2014/2015 The if s that are conceptually

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators

Introduction to Decision Structures. Boolean & If Statements. Different Types of Decisions. Boolean Logic. Relational Operators Boolean & If Statements Introduction to Decision Structures Chapter 4 Fall 2015, CSUS Chapter 4.1 Introduction to Decision Structures Different Types of Decisions A decision structure allows a program

More information