MATLAB Selection Statements and Logical Operators, Study notes of Computer Programming

This chapter from Attaway MATLAB 4E covers selection statements, including if and if-else, and logical operators such as &&, ||, and ~. Topics include relational expressions, exchanging variable values, representing true/false concepts, and common pitfalls.

Typology: Study notes

2021/2022

Uploaded on 09/07/2022

adnan_95
adnan_95 🇮🇶

4.3

(39)

918 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 4
Attaway MATLAB 4E
Selection Statements
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download MATLAB Selection Statements and Logical Operators and more Study notes Computer Programming in PDF only on Docsity!

Chapter 4 Attaway MATLAB 4E Selection Statements

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

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 are not always necessary! — Simplify this statement: if num < 0 num = 0; else num = num; end — Answer: if num < 0 num = 0; end — 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

The elseif clause

— MATLAB also has an elseif clause which shortens the code (and cuts down on the number of ends) — General form: if condition action elseif condition action elseif condition action % 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: if val >= 4 disp('ok') else disp('smaller') end — 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 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

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.