Programming with MATLAB Relational Operators Relational Operators n
Programming with MATLAB
Relational Operators
Relational Operators n The arithmetic operators has precedence over relational operators
Logical Operators
Order of precedence for operators
Order of precedence for operators
Logical variables n Logical variables can take only two values: q q <> 0 (true) 0 (false)
The if statement Start End
The if statement n n 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 n n 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
The else statement
The else statement n n 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 n Write a program to display your course letter grade knowing your numeric grade q Example: n n 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 =
Nested if statement = n This statement gives the same result as the previous one
Examples n n 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 q q 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
- Slides: 20