Go back to user prompt in a switch case if no case is valid (Looping the switch statement)
2 views (last 30 days)
Show older comments
So on previouis software I always used GOTO function for easier loops, but since GOTO function is not existed in Matlalb, I try to use while for a program where when a user prompt is not applicable to any case listed in the switch statement, the program will directed back to the user prompt until one of the case is applicable.
msgbox('Please state your condition before taking the glucose reading');
msgbox(["Please press: "; " '1' if you were fasting. ";" '2' if you took the reading around 2 hours after meal. "]);
while true
n = inputdlg();
nnum = str2double(n);
switch nnum
case 1
gread = inputdlg('Please input your glucose reading: ');
greadnum = str2double(gread);
if greadnum < 70
msgbox('Your glucose level is too low.');
elseif (70<=greadnum) && (greadnum<=100)
msgbox('Congratulations, your glucose level is normal.');
elseif(101<=greadnum)&&(greadnum<=125)
msgbox('You are diagnosed as pre-diabetes, please have a more healthier lifestyle.');
else
msgbox('You are diagnosed as diabetes. Please seek medical assisstance as soon as possible.');
end
case 2
gread = inputdlg('Please input your glucose reading: ');
greadnum = str2double(gread);
if greadnum < 70
msgbox('Your glucose level is too low.');
elseif (70<=greadnum) && (greadnum<140)
msgbox('Congratulations, your glucose level is normal.');
elseif(140<=greadnum)&&(greadnum<=200)
msgbox('You are diagnosed as pre-diabetes');
else
msgbox('You are diagnosed as diabetes');
end
otherwise
msgbox("Please input correct value");
end
end
Since the only discusion I found is this, I tried to run the program, however the program seems to be in infinite loop, no matter if I choose case '1', '2' or otherwise, it will always go back to the user prompt under the 'while' line. Is there a way that I can fix it so the loop can end if I choose either case '1' or '2'?
Also,the program sasid that the switch must be a scalar or a character vector, but I already have change the user prompt in inputdlg into a num using the str2num function
Thank you in advance
0 Comments
Answers (1)
Dyuman Joshi
on 15 Apr 2023
"however the program seems to be in infinite loop"
Because you have not added any break if any of the condition is satisfied.
The error (most likely) occurs because you did not provide any value to the input dialogue box in the 2nd iteration of the loop (as you closed the dialogue box after the 1st succesful iteration) resulting in nnum being an empty double.
I have added a break in your code -
msgbox('Please state your condition before taking the glucose reading');
msgbox(["Please press: "; " '1' if you were fasting. ";" '2' if you took the reading around 2 hours after meal. "]);
%condition counter for the loop
y=true;
%Run the loop while y is true
while y
n = inputdlg();
nnum = str2double(n);
switch nnum
case 1
gread = inputdlg('Please input your glucose reading: ');
greadnum = str2double(gread);
if greadnum < 70
msgbox('Your glucose level is too low.');
elseif (70<=greadnum) && (greadnum<=100)
msgbox('Congratulations, your glucose level is normal.');
elseif(101<=greadnum)&&(greadnum<=125)
msgbox('You are diagnosed as pre-diabetes, please have a more healthier lifestyle.');
else
msgbox('You are diagnosed as diabetes. Please seek medical assisstance as soon as possible.');
end
%Update y to be false as the condition is satisfied
y=false;
case 2
gread = inputdlg('Please input your glucose reading: ');
greadnum = str2double(gread);
if greadnum < 70
msgbox('Your glucose level is too low.');
elseif (70<=greadnum) && (greadnum<140)
msgbox('Congratulations, your glucose level is normal.');
elseif(140<=greadnum)&&(greadnum<=200)
msgbox('You are diagnosed as pre-diabetes');
else
msgbox('You are diagnosed as diabetes');
end
%Update y to be false as the condition is satisfied
y=false;
otherwise
msgbox("Please input correct value");
end
end
0 Comments
See Also
Categories
Find more on Biological and Health Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!