Looping a switch statement

30 ビュー (過去 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
  1 件のコメント
Bob Sherland
Bob Sherland 2018 年 4 月 24 日
Thanks so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by