Q:

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”

0

 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). 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch4Ex17.m

% Prompts the user for a 'y' or 'n' answer and responds

% accordingly, using an if-else statement

letter = input('Enter your answer: ', 's');

if letter == 'y' || letter == 'Y'

 disp('OK, continuing')

elseif letter == 'n' || letter == 'N'

 disp('OK, halting')

else

 disp('Error')

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now