Exercise 5
In a script, the user is supposed to enter either a 'y' or 'n' in response to a prompt. The user's input is read into a character variable called letter. The script will print "OK, continuing" if the user enters either a 'y' or 'Y' or it will print "OK, halting" if the user enters an 'n' or 'N' or "Error" if the user enters anything else.
letter = input('Enter your answer: ', 's');
Note: The original text shows 'a' as the second argument, but in modern MATLAB, 's' is used to read string input. The variable assignment operator should be = instead of -.
Requirements:
- Use the provided input statement
- Implement using a single nested if-else statement
elseif clause is permitted
- Handle both uppercase and lowercase 'y' and 'n'
- Display appropriate messages based on user input
Program Logic Flow:
- Read user input into variable
letter
- Check if input is 'y' or 'Y' → print "OK, continuing"
- Else, check if input is 'n' or 'N' → print "OK, halting"
- Else (any other input) → print "Error"
Programming Constraints:
- Must use single nested if-else statement
- Cannot use multiple separate if statements
- Cannot use
switch-case statement
- Must handle case sensitivity ('y', 'Y', 'n', 'N')
Example Input/Output:
Example 1:
Enter your answer: 'y'
OK, continuing
Example 2:
Enter your answer: 'Y'
OK, continuing
Example 3:
Enter your answer: 'n'
OK, halting
Example 4:
Enter your answer: 'N'
OK, halting
Example 5:
Enter your answer: 'x'
Error
Example 6:
Enter your answer: 'yes'
Error
Expected Code Structure:
letter = input('Enter your answer: ', 's');
if _____
fprintf('OK, continuing\n');
elseif _____
fprintf('OK, halting\n');
else
fprintf('Error\n');
end
Hint: Remember you can use the logical OR operator | to check for multiple conditions, like: (letter == 'y') | (letter == 'Y')