Exercise 3
Write a statement that will store logical true in a variable named
isit if the value of a variable x is in the range from 0 to 10, or logical false if not. Do this with just one assignment statement, with no if or if-else statement!
Requirements:
- Create a variable named
isit - Check if variable
xis in the range [0, 10] - Store
logical trueif x is in range - Store
logical falseif x is not in range - Use only one assignment statement
Constraints:
- No
ifstatements - No
if-elsestatements - Only one assignment to
isit
Hint:
Think about using logical operators and comparison operators together in a single expression. Remember that in MATLAB, you can combine multiple conditions using & (AND) operator.
Expected Behavior Examples:
- If
x = 5, thenisitshould betrue - If
x = 0, thenisitshould betrue - If
x = 10, thenisitshould betrue - If
x = -1, thenisitshould befalse - If
x = 11, thenisitshould befalse
Advanced Hint:
Consider what happens when you write: (x ≥ 0) & (x ≤ 10). This expression will evaluate to logical true or false based on the value of x.
To take full advantage, try implementing the exercise before checking the solutions!