Programming with MATLAB
Relational Operators
The arithmetic operators has precedence over relational operators
Logical Operators
Order of precedence for operators
Logical variables Logical variables can take only two values: <> 0 (true) 0 (false)
The if statement End Start
The if statement If x is greater than or equal 5, the statement y = sqrt(x-5) gets executed Otherwise, the statement does not execute
The if statement The value of z and w are computed only if both x and y are nonnegative. If any of x or y is negative, both w and z retain their initial values
The else statement
If x is nonnegative, y = sqrt(x) is executed If x is negative, an error message is displayed
The elseif statement
Example: Numeric grade to letter grade Write a program to display your course letter grade knowing your numeric grade Example: If your numeric grade is 93, then the program displays the letter A If your numeric grade is 70, then the program displays the letter C
Nested if statement =
This statement gives the same result as the previous one =
Examples Write a function that accepts one variable, which is the year and returns a logical variable: true (if the year is leap) or false (if the year is not leap) A leap year is characterized by All years evenly dividable by 400 are leap All years that are evenly dividable by 4 and are not evenly dividable by 100 are leap
Examples