Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a script, the user is supposed to enter either a \'y\' or \'n\' in response t

ID: 3826178 • Letter: I

Question

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 a 'n' or 'N' or "Error" if the user enters anything else. Put this statement in the script first: letter = input ('Enter your answer: ', 's'); Write the script using a single nested if-else statement (elseif clause is permitted). Write the script from the previous exercise using a switch statement instead.

Explanation / Answer

letter = input('Enter your answer: ', 's');
if letter == 'y' || letter == 'Y'
   fprintf('Ok, continuing...');
elseif letter == 'n' || letter == 'N'
   fprintf('Ok, halting...');
else
   fprintf('Error');
end

switch letter
   case {'Y', 'y'}
       fprintf('Ok, continuing...');
   case {'N', 'n'}
       fprintf('Ok, halting...');              
   otherwise
       warning('Error');

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote