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

Size: px
Start display at page:

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

Transcription

1 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? A. The first line of a function file must start with the word function, followed by the name of the function and a list of input and output arguments, following a specified format. B. Script variables share scope with the main Matlab workspace, while function variables have their own local scope. C. Functions are scripts that have been pre-compiled into binary format, so that they run faster and are not available for inspection by users. D. Both A and B. E. Both A and C. 2 1

2 A Common Iterative Approach Initialize Problem Set initial solution % Calculate initial solution % Estimate initial error for counter = 1:limit Current solution good enough? No Max # of iterations reached? No Improve solution Yes Yes Report best results found if( abs( error ) < tol ) break; % Improve solution % Estimate error or here % Report best solution found, % # of iterations, and estimated error. 3 Branches and Loops Branches ( decision making ) and loops are two of the most important constructs in computer programming. Branches allow code to optionally execute different code ( or none at all ) deping on the values of variables and other criteria. Loops make code repeat execution, possibly millions of times per second. 4 2

3 Matlab if-else Construct Syntax: if( condition ) % code to execute if true else % else and false code are optional % code to execute if false % This line is required, to mark of if If the condition is a matrix, it is considered to be true if any part of it is true ( non-zero ). 5 Matlab Relational Operators Relational operators a < b a > b a <= b a >= b a == b a ~= b Result is true if and only if: a is less than b a is greater than b a is less than or equal to b a is greater than or equal to b a is equal to b a is not equal to b No spaces allowed in <=, >=, ==, or ~= WARNING: Do not confuse == with =! If a and b are matrices, then result is a matrix. 6 3

4 Combined Ifs Either the true or false clauses of an if may contain any other code, including other ifs. When the true clause of an if contains another if, it is termed nested. When the false clause of an if contains another if, it is termed sequential. It is not uncommon to see long strings of sequential ifs. 7 A Nested If Example if( X <= 0.0 ) if( X < 0.0 ) fprintf( Negative ); else fprintf( Zero ); else fprintf( Positive ); 8 4

5 Sequential Ifs in Matlab Matlab has an elseif keyword for use in sequential ifs: if( condition ) % code to execute if true elseif( condition2 ) % Any number of these, optional % code to execute if sequential condition2 is true else % else and false code are optional % code to execute if false % This is required 9 A Sequential If Example if( X < 0.0 ) fprintf( Negative ); elseif( X > 0.0 ) fprintf( Positive ); else fprintf( Zero ); 10 5

6 A Longer Sequential If Example if( score >= 90.0) fprintf( A ); elseif( score >= 80.0) fprintf( B ); elseif( score >= 70.0) fprintf( C ); elseif( score >= 60.0) fprintf( D ); else fprintf( F ); Note that the order of the tests is important in this example. 11 In a series of sequential ifs, How many clauses will be executed? A. Every one that is true. B. The first one that is true. C. One of the true ones, but which one may be machine and compiler depent. D. An error will occur if more than one clause is true. E. The first one if it is true, otherwise none at all. 12 6

7 Matlab Logical Operators Logical operator Equivalent function Description a & b a b and(a,b) or(a,b) ~a not(a) xor(a,b) Returns true if a and b are true, meaning both are true. Returns true if a or b is true, meaning either is true (or both). Negates or complements the value of a, returning true if a is false, and vice versa. Returns true if a is true or b is true but not both, i.e., if exactly one of a or b is true. Known as exclusive or or xor (the x comes from exclusive). 13 & vs. && and vs. Matlab has two forms of the logical AND and OR operators, using one or two symbols. When dealing with matrices, the single-character versions act element-by-element, producing a matrix of results. The double-character versions are only usable with scalars, and perform short-circuit evaluation, stopping as soon as the result is known. Example: if( x ~= 0 && 1.0 / x > tol ) won t divide by

8 Precedence for Logical Operators Convention Description Explanation ( ) Items within parentheses are evaluated first. ~ not (negation) is evaluated next. & and is evaluated after not. Last to be evaluated is or. left-to-right If more than one operator of equal precedence could be evaluated, evaluation occurs left to right. For example, ~ a & b evaluates as (~a) & b. Thus a & b c evaluates as (a & b) c. The expression will always be true if both and and b are true, for example. Thus, a b c evaluates as (a b) c. 15 How do you check if X is between 0 and 10? A. if( 0.0 < X < 10.0 ) B. if( 0.0 <= X <= 10.0 ) C. if( 0.0 < X && X < 10.0 ) D. if( 0.0 <= X && X <= 10.0 ) E. if( 0.0 < X X < 10.0 ) 16 8

9 Matlab Relational & Logical Functions Function any(x) isnan(x) isfinite(x) isinf(x) Description Returns true if x is nonzero; otherwise, returns false. Returns true if x is NaN (Not-a- Number); otherwise, returns false. Returns true if x is finite; otherwise, returns false. For example, isfinite(inf) is false, and isfinite(10) is true. Returns true if x is +Inf or -Inf; otherwise, returns false. 17 Matlab While Loop Syntax while( condition ) % code in body of loop % required Matlab does not have do-while, but does have break & continue to be explained later. condition? True body Following code False 19 9

10 Input Checking With a While Loop age = -1; % Initialized to guarantee loop entry while( age < 0 age > 120 ) age = input( Please enter your age: ); if( age < 0 age > 120 ) fprintf( %d is invalid. Try again.\n, age ); fprintf( Valid ages are from 0 to 120.\n ); % Continue while input is bad 20 Matlab for loops Syntax: for variable = List_of_Values % Code to execute for each value on the list. % required The loop will execute as many times as there are values on the list, with variable taking on a separate value from the list each iteration. Sample lists: 1:10, 0:0.1*pi:pi, [ 3, 5, 8, 11 ], X 21 10

11 Break and Continue break causes a loop to finish immediately, continuing execution with the code following the loop body. continue causes the current iteration of a loop to finish, starting the next iteration. While loops will jump to the evaluation of the loop condition. For loops will set the loop variable to the next item on the list, provided there are items left. 22 For Loop Flowchart Illustrating break and continue for var in list % code 1 if( test_c ) continue; % code 2 if( test_b ) break; % code 3 set var to next item init var to 1 st list item True items remain? code 1 test_c code 2 test_b code 3 Following code True False 23 11

12 Input Checking With an Infinite Loop while( true ) % Infinite loop, until good input is given age = input( Please enter your age: ); if( age >= 0 && age <= 120 ) break; % Exit loop only when good input fprintf( %d is invalid. Try again.\n, age ); fprintf( Valid ages are from 0 to 120.\n ); % Continue while input is bad 24 Matlab Loops and Arrays It is generally NOT a good idea to use loops to access Matlab array elements. Instead use the power of Matlab to operate on entire arrays ( or subsets thereof ) in one operation. If you must use a loop to build an array element-by-element, pre-size it first to speed up operations, instead of growing the array by one element each loop iteration

13 Consider a case where a variable is checked against a list of choices: direction = input( Which way?, NSEW?, s ); if( direction == W ) % Go West X = X - 1; elseif ( direction == E ) % East X = X + 1; else if ( direction == N ) % North Y = Y + 1; else if( direction == S ) % South Y = Y - 1; else fprintf( Illegal Move.\n ); switch( direction ) case W % Go West X = X - 1; case E % East X = X + 1; case N % North Y = Y + 1; case S % South Y = Y - 1; otherwise fprintf( Illegal Move. \n ); 26 Matlab switch construct Syntax: switch( expression ) case caseexpr1 % code to execute if expression == caseexpr1 % Add additional cases as needed otherwise % code to execute if no case expressions match % required Case expressions must be scalars. ( or strings or cells ) No fall-through from one case to the next

14 Review Which of the following C++ logic constructs is NOT available in Matlab? A. if-else construct B. for loops C. while loops D. do-while loops E. switch construct 29 14

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

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

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

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

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

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

Chapter 4: Making Decisions

Chapter 4: Making Decisions 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

Chapter 4: Making Decisions

Chapter 4: Making Decisions Chapter 4: Making Decisions CSE 142 - Computer Programming I 1 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >=

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

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

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

Functions of Two Variables

Functions of Two Variables Functions of Two Variables MATLAB allows us to work with functions of more than one variable With MATLAB 5 we can even move beyond the traditional M N matrix to matrices with an arbitrary number of dimensions

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

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing SECTION 5: STRUCTURED PROGRAMMING IN MATLAB ENGR 112 Introduction to Engineering Computing 2 Conditional Statements if statements if else statements Logical and relational operators switch case statements

More information

LECTURE 04 MAKING DECISIONS

LECTURE 04 MAKING DECISIONS PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 04 MAKING DECISIONS

More information

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators.

Relational and Logical Operators. MATLAB Laboratory 10/07/10 Lecture. Chapter 7: Flow Control in Programs. Examples. Logical Operators. Relational and Logical Operators MATLAB Laboratory 10/07/10 Lecture Chapter 7: Flow Control in Programs Both operators take on form expression1 OPERATOR expression2 and evaluate to either TRUE (1) or FALSE

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

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

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

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script. LESSON 3 Flow Control In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script. In this chapter we ll look at two types of flow control:

More information

Relational & Logical Operators

Relational & Logical Operators Relational & Logical Operators ELEC 206 Prof. Siripong Potisuk 1 Objectives Learn how to change the flow of execution of a MATLAB program through some kind of a decision-making process within that program

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

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

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial 1 Matlab Tutorial 2 Lecture Learning Objectives Each student should be able to: Describe the Matlab desktop Explain the basic use of Matlab variables Explain the basic use of Matlab scripts Explain the

More information

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Ordinary Differential Equation Solver Language (ODESL) Reference Manual Ordinary Differential Equation Solver Language (ODESL) Reference Manual Rui Chen 11/03/2010 1. Introduction ODESL is a computer language specifically designed to solve ordinary differential equations (ODE

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

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

H.C. Chen 1/24/2019. Chapter 4. Branching Statements and Program Design. Programming 1: Logical Operators, Logical Functions, and the IF-block 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

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. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

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

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

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

Repetition Structures

Repetition Structures Repetition Structures Chapter 5 Fall 2016, CSUS Introduction to Repetition Structures Chapter 5.1 1 Introduction to Repetition Structures A repetition structure causes a statement or set of statements

More information

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards Language Reference Manual Introduction The purpose of

More information

CS227-Scientific Computing. Lecture 3-MATLAB Programming

CS227-Scientific Computing. Lecture 3-MATLAB Programming CS227-Scientific Computing Lecture 3-MATLAB Programming Contents of this lecture Relational operators The MATLAB while Function M-files vs script M-files The MATLAB for Logical Operators The MATLAB if

More information

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

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

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

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

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

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

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation

3. Logical Values. Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation 140 3. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation Our Goal 141 int a; std::cin >> a; if (a % 2 == 0) std::cout

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution. Repetition Structures Introduction to Repetition Structures Chapter 5 Spring 2016, CSUS Chapter 5.1 Introduction to Repetition Structures The Problems with Duplicate Code A repetition structure causes

More information

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators

Flow Control. Statements We Will Use in Flow Control. Statements We Will Use in Flow Control Relational Operators Flow Control We can control when how and the number of times calculations are made based on values of input data and/or data calculations in the program. Statements We Will Use in Flow Control for loops

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

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

Repetition Structures Chapter 9

Repetition Structures Chapter 9 Sum of the terms Repetition Structures Chapter 9 1 Value of the Alternating Harmonic Series 0.9 0.8 0.7 0.6 0.5 10 0 10 1 10 2 10 3 Number of terms Objectives After studying this chapter you should be

More information

Computational Physics - Fortran February 1997

Computational Physics - Fortran February 1997 Fortran 90 Decision Structures IF commands 3 main possibilities IF (logical expression) IF (logical expression) THEN IF (logical expression) THEN IF (logical expression) THEN expression TRUE expression

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

Programming in MATLAB

Programming in MATLAB 2. Scripts, Input/Output and if Faculty of mathematics, physics and informatics Comenius University in Bratislava October 7th, 2015 Scripts Scripts script is basically just a sequence of commands the same

More information

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved. 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

This is the basis for the programming concept called a loop statement

This is the basis for the programming concept called a loop statement 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 did you solve it? What if you had millions of data points

More information

Checking Multiple Conditions

Checking Multiple Conditions Checking Multiple Conditions Conditional code often relies on a value being between two other values Consider these conditions: Free shipping for orders over $25 10 items or less Children ages 3 to 11

More information

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme Chapter 4: 4.1 Making Decisions Relational Operators Simple Program Scheme Simple Program Scheme So far our programs follow a simple scheme Gather input from the user Perform one or more calculations Display

More information

XQ: An XML Query Language Language Reference Manual

XQ: An XML Query Language Language Reference Manual XQ: An XML Query Language Language Reference Manual Kin Ng kn2006@columbia.edu 1. Introduction XQ is a query language for XML documents. This language enables programmers to express queries in a few simple

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 5 5 Controlling the Flow of Your Program Control structures allow a programmer to define how and when certain statements will

More information

CS 1313 Spring 2000 Lecture Outline

CS 1313 Spring 2000 Lecture Outline 1. What is a Computer? 2. Components of a Computer System Overview of Computing, Part 1 (a) Hardware Components i. Central Processing Unit ii. Main Memory iii. The Bus iv. Loading Data from Main Memory

More information

Quiz 1: Functions and Procedures

Quiz 1: Functions and Procedures Quiz 1: Functions and Procedures Outline Basics Control Flow While Loops Expressions and Statements Functions Primitive Data Types 3 simple data types: number, string, boolean Numbers store numerical data

More information

(Not Quite) Minijava

(Not Quite) Minijava (Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing SECTION 1: INTRODUCTION ENGR 112 Introduction to Engineering Computing 2 Course Overview What is Programming? 3 Programming The implementation of algorithms in a particular computer programming language

More information

Logic, loops and control flow. NENS 230 Eddy Albarran

Logic, loops and control flow. NENS 230 Eddy Albarran 1 Logic, loops and control flow NENS 230 Eddy Albarran 10.6.15 2 Announcements Eddy s office hours this week: Weds 11AM-12:30PM Email to schedule other time(s) 3 Outline for today Review of relational

More information

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program

More information

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++ 148 Our Goal 149 3. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation int a; std::cin >> a; if (a % 2 == 0) std::cout

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

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++ Our Goal 3. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation int a; std::cin >> a; if (a % 2 == 0) std::cout

More information

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie

21-Loops Part 2 text: Chapter ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie 21-Loops Part 2 text: Chapter 6.4-6.6 ECEGR 101 Engineering Problem Solving with Matlab Professor Henry Louie While Loop Infinite Loops Break and Continue Overview Dr. Henry Louie 2 WHILE Loop Used to

More information

Cellular Automata Language (CAL) Language Reference Manual

Cellular Automata Language (CAL) Language Reference Manual Cellular Automata Language (CAL) Language Reference Manual Calvin Hu, Nathan Keane, Eugene Kim {ch2880, nak2126, esk2152@columbia.edu Columbia University COMS 4115: Programming Languages and Translators

More information

Quick Reference Guide

Quick Reference Guide SOFTWARE AND HARDWARE SOLUTIONS FOR THE EMBEDDED WORLD mikroelektronika Development tools - Books - Compilers Quick Reference Quick Reference Guide with EXAMPLES for Basic language This reference guide

More information

Chapter 2: Functions and Control Structures

Chapter 2: Functions and Control Structures Chapter 2: Functions and Control Structures TRUE/FALSE 1. A function definition contains the lines of code that make up a function. T PTS: 1 REF: 75 2. Functions are placed within parentheses that follow

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

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk Control Statements ELEC 206 Prof. Siripong Potisuk 1 Objectives Learn how to change the flow of execution of a MATLAB program through some kind of a decision-making process within that program The program

More information

Einführung in die Programmierung Introduction to Programming

Einführung in die Programmierung Introduction to Programming Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5 Today Attributes, formal arguments, and local variables Control structures

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

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

MATLIP: MATLAB-Like Language for Image Processing

MATLIP: MATLAB-Like Language for Image Processing COMS W4115: Programming Languages and Translators MATLIP: MATLAB-Like Language for Image Processing Language Reference Manual Pin-Chin Huang (ph2249@columbia.edu) Shariar Zaber Kazi (szk2103@columbia.edu)

More information

Introduction to C/C++ Lecture 3 - Program Flow Control

Introduction to C/C++ Lecture 3 - Program Flow Control Introduction to C/C++ Lecture 3 - Program Flow Control Rohit Sehgal Nishit Majithia Association of Computing Activities, Indian Institute of Technology,Kanpur rsehgal@cse.iitk.ac.in nishitm@cse.iitk.ac.in

More information

1.00 Lecture 5. Floating Point Anomalies

1.00 Lecture 5. Floating Point Anomalies 1.00 Lecture 5 More on Java Data Types, Control Structures Introduction to Methods Reading for next time: Big Java: 7.1-7.5, 7.8 Floating Point Anomalies Anomalous floating point values: Undefined, such

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

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

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) Introduction ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah) MATLAB is a powerful mathematical language that is used in most engineering companies today. Its strength lies

More information

CH6: Programming in MATLAB

CH6: Programming in MATLAB CH6: Programming in MATLAB 1- Relational and Logical operators: Relational operators: Examples Result >> 5>8 0 >> a= 6~=2 a=1 >> b= (3>2)+(5*2==10*1)*(32>=128/4) b=2 >> c= 8/(29) c=?? Order of

More information

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

Lecture 15 MATLAB II: Conditional Statements and Arrays

Lecture 15 MATLAB II: Conditional Statements and Arrays Lecture 15 MATLAB II: Conditional Statements and Arrays 1 Conditional Statements 2 The boolean operators in MATLAB are: > greater than < less than >= greater than or equals

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

Lecture 5 Tao Wang 1

Lecture 5 Tao Wang 1 Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,

More information

Lecture 7 Tao Wang 1

Lecture 7 Tao Wang 1 Lecture 7 Tao Wang 1 Objectives In this chapter, you will learn about: Interactive loop break and continue do-while for loop Common programming errors Scientists, Third Edition 2 while Loops while statement

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017 Loops! Loops! Loops! Lecture 5 COP 3014 Fall 2017 September 25, 2017 Repetition Statements Repetition statements are called loops, and are used to repeat the same code mulitple times in succession. The

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

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

1 Overview of the standard Matlab syntax

1 Overview of the standard Matlab syntax 1 Overview of the standard Matlab syntax Matlab is based on computations with matrices. All variables are matrices. Matrices are indexed from 1 (and NOT from 0 as in C!). Avoid using variable names i and

More information

Selections. CSE 114, Computer Science 1 Stony Brook University

Selections. CSE 114, Computer Science 1 Stony Brook University Selections CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation If you assigned a negative value for radius in ComputeArea.java, then you don't want the

More information

ECE 202 LAB 3 ADVANCED MATLAB

ECE 202 LAB 3 ADVANCED MATLAB Version 1.2 1 of 13 BEFORE YOU BEGIN PREREQUISITE LABS ECE 201 Labs EXPECTED KNOWLEDGE ECE 202 LAB 3 ADVANCED MATLAB Understanding of the Laplace transform and transfer functions EQUIPMENT Intel PC with

More information

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School Objectives Construct and evaluate Boolean expressions Understand how to use selection statements to make decisions Design and test

More information