Looping a switch statement

조회 수: 29 (최근 30일)
Bob Sherland
Bob Sherland 2018년 4월 24일
댓글: Bob Sherland 2018년 4월 24일
I currently have the code below, in which the user is asked to enter a task, which then uses the switch statement shown below.
switch task
case 'create a file'
fprintf('You have chosen to create a file \n');
case 'quit'
fprintf('You have chosen to quit \n');
break;
otherwise
fprintf('You have entered an action not on the list\n');
fprintf('You will now be directed to the start again to select another action \n');
I was wondering how to loop it back to the beginning and continually ask for the user to input a task and how to exit fully out of the loop if the user enters 'quit'

채택된 답변

Ahmet Cecen
Ahmet Cecen 2018년 4월 24일
편집: Ahmet Cecen 2018년 4월 24일
This is not the safest way to do this, and you will likely realize why later, but for purely educational purposes:
while true
task = input('Please Enter Task\n','s');
switch task
case 'create a file'
fprintf('You have chosen to create a file \n');
case 'quit'
fprintf('You have chosen to quit \n');
break;
otherwise
fprintf('You have entered an action not on the list\n');
fprintf('You will now be directed to the start again to select another action \n');
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by