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

MATLAB The user is supposed to enter either a ‘yes’ or ‘no’ in response to a pro

ID: 2086368 • Letter: M

Question

MATLAB

The user is supposed to enter either a ‘yes’ or ‘no’ in response to a prompt. The script will print “OK, continuing” if the user enters either a ‘y’, ‘Y’,'Yes', or 'yes' or it will print “OK, halting” if the user enters a ‘n’, ‘N’, 'No', or 'no' or will print “Error” if the user enters anything else.

Code this twice once with if and once with switch:

Then recode the example above to keep prompting until an an acceptable yes or no is entered:

What loop should we use?

what should we intialize?

please generate all code for Matlab

Explanation / Answer

If Part Code:-

prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

if (input_user =='y') || (input_user =='Y') || (input_user =='yes') || (input_user =='YES')
disp("OK, continue");
elseif(input_user =='n') || (input_user =='N') || (input_user =='no') || (input_user =='NO')
disp("OK, halting");
else
disp("Error");
end

Output:-

Do you want to continue or not =Y
OK, continue

Swich Code

clc
clear all
prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

switch ( input_user)
case {'Y','y','YES','yes'}
    disp("OK, Continue")
case {'N','n','NO','no'}
    disp("OK, Halting")
   otherwise
      disp("Error")
end

Output

Do you want to continue or not =N
OK, Halting

if loop Code:-

clear all
clc
while(1)
prompt ="Do you want to continue or not =";
input_user = input(prompt,"s");

if (input_user =='y') || (input_user =='Y') || (input_user =='yes') || (input_user =='YES')
disp("OK, continue");
break;
elseif (input_user =='n') || (input_user =='N') || (input_user =='no') || (input_user =='NO')
disp("OK, halting");
break;
else
disp("Error");
end
end

Output

Do you want to continue or not =g
Error
Do you want to continue or not =y
OK, continue

swich loop Code

clc
clear all
while(1)
prompt ="Do you want to continue or not =";
input_user = input(prompt,'s');

switch ( input_user)
case {'Y','y','YES','yes'}
    disp("OK, Continue")
    break
case {'N','n','NO','no'}
    disp("OK, Halting")
    break;
   otherwise
      disp("Error")
end

end


Output

Do you want to continue or not =g
Error
Do you want to continue or not =y
OK, Continue