Selection Statements Chapter 4 Attaway MATLAB 4E.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Conditional Statements Introduction to Computing Science and Programming I.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
If/Else Statements.
Chapter 4: Decisions and Conditions
EEE 161 Applied Electromagnetics
CprE 185: Intro to Problem Solving (using C)
Chapter 4 The If…Then Statement
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
EGR 2261 Unit 4 Control Structures I: Selection
Operator Precedence Operators Precedence Parentheses () unary
The Selection Structure
Selection Statements by Ahmet Sacan
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Scripts & Functions Scripts and functions are contained in .m-files
Conditional Statements
More Selections BIS1523 – Lecture 9.
Topics The if Statement The if-else Statement Comparing Strings
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 3: Program Statements
VB Decisions, Conditions & If
Chapter 3: Program Statements
If selection construct
Logical Operations In Matlab.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Chapter 3: Selection Structures: Making Decisions
CS2011 Introduction to Programming I Selections (I)
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Selection Statements Chapter 3.
The Selection Structure
CprE 185: Intro to Problem Solving (using C)
Selection Statements Chapter 4 Attaway MATLAB 5E.
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Selection Statements Chapter 4 Attaway MATLAB 4E

Recall Relational Expressions The relational operators in MATLAB are: > greater than < less than >= greater than or equals <= less than or equals == equality ~= inequality The resulting type is logical 1 for true or 0 for false The logical operators are: || or for scalars && and for scalars ~ not Also, xor function which returns logical true if only one of the arguments is true

If Statement The if statement is used to determine whether or not a statement or group of statements is to be executed General form: if condition action end the condition is any relational expression the action is any number of valid statements (including, possibly, just one) if the condition is true, the action is executed – otherwise, it is skipped entirely

Exchanging values of variables Useful, for example, when the value of one variable is supposed to be less than another – if that is not the case, exchange their values (so, an if statement is used to determine whether this is necessary or not) A temporary variable is necessary Algorithm to exchange values of variables a and b: Assign the value of a to temp Assign the value of b to a Assign the value of temp to b

Representing true/false concepts Note: to represent the concept of false, 0 is used. To represent the concept of true, any nonzero value can be used – so expressions like 5 or ‘x’ result in logical true This can lead to some common logical errors For example, the following expressions are always true (because the “relational expressions” on the right, 6 and ‘N’, are nonzero so they are true; therefore, it does not matter what the results of the others are): number < 5 || 6 letter == ‘n’ || ‘N’

If-else Statements The if-else statement chooses between two actions General form: if condition action1 else action2 end One and only one action is executed; which one depends on the value of the condition (action1 if it is logical true or action2 if it is false)

If-else statements are not always necessary! Simplify this statement: if num < 0 num = 0; else num = num; end Answer: The point is that the else clause does not accomplish anything, so it is not necessary … sometimes just an if statement is all you need!

Throwing an error MATLAB has an error function that can be used to display an error message in red, similar to the error messages generated by MATLAB if radius <= 0 error('Sorry; %.2f is not a valid radius\n', radius) else % carry on end

Nested if-else Statements To choose from more than two actions, nested if-else statements can be used (an if or if-else statement as the action of another) General form: if condition1 action1 else if condition2 action2 if condition3 action3 % etc: there can be many of these actionn % the nth action end

The elseif clause MATLAB also has an elseif clause which shortens the code (and cuts down on the number of ends) General form: if condition1 action1 elseif condition2 action2 elseif condition3 action3 % etc: there can be many of these else actionn % the nth action end

The elseif clause is not always necessary! Simplify this statement: if val >= 4 disp('ok') elseif val < 4 disp('smaller') end Answer: else The point is that if you get to the else clause, you know that the expression val >= 4 is false – so, val must be less than 4 so there is no need to check that.

The switch Statement The switch statement can frequently be used in place of a nested if statement General form: switch switch_expression case caseexp1 action1 case caseexp2 action2 case caseexp3 action3 % etc: there can be many of these otherwise actionn end this can be used when comparing the switch_expression to see if it is equal to the values on the case labels (the otherwise clause handles all other possible values)

The menu function The menu function displays a menu of push-buttons, and returns the result of the button push (1 for the first button, 2 for the second, etc. – or 0 if no button is pushed) Then, a switch or nested if statement isused to perform different actions based on the menu options As of R2015b, this function is no longer recommended

The “is” functions There are many “is” functions in MATLAB that essentially ask a true/false question, and return logical 1 for true or 0 for false isletter returns 1 or 0 for every character in a string – whether it is a letter of the alphabet or not isempty returns 1 if the variable argument is empty, or 0 if not iskeyword returns 1 if the string argument is a keyword, or 0 if not isa determines whether the first argument is a specified bype

Common Pitfalls Some common pitfalls have been pointed out already; others include: Using = instead of == for equality in conditions Putting a space in the keyword elseif Not using quotes when comparing a string variable to a string, such as letter == y instead of letter == 'y' Writing conditions that are more complicated than necessary, such as  if (x < 5) == 1 instead of just  if (x < 5)

Programming Style Guidelines Use indentation to show the structure of a script or function. In particular, the actions in an if statement should be indented. When the else clause isn’t needed, use an if statement rather than an if-else statement When using the menu function, ensure that the program handles the situation when the user clicks on the red ‘X’ on the menu box rather than pushing one of the buttons.