Problem with a loop
2 views (last 30 days)
Show older comments
we are doing a project and I have problems with the main loop of the system, basically I want the loop to repeat itself if the answer is not within the range of our variable and for the loop to continue and exit the loop if it is within the range, also if the answer is correct the program has to ask if you are ok with your answer. So the problem comes when I put the wrong answer (the value is over the range of the variable) it does ask if you are ok with your answer and it is not suposed to do that
I also have to say this is not an entire project, is just a little part of it
function progwtf
repeat = 'yes';
while strcmp(repeat,'yes')|| strcmp(repeat,'YES')
a = -1;
while a<1
a = input('which is your height? (meters)');
if a<1.00
disp('that is not posible')
elseif a>2.30
disp('that is not posible')
end
end
repeat = input('Do you want to change it? (yes/no)','s');
end
If someone is kind enought to answer me pls do
0 Comments
Accepted Answer
Voss
on 25 Mar 2022
repeat = 'yes';
while strcmpi(repeat,'yes') % use strcmpi for case-insensitive comparison
while true % keep looping until a break statement is encountered
a = input('what is your height? (meters)');
if a<1.00 || a>2.30
disp('that is not likely'); % note: people can be very tall or very short (e.g., young children are short, typically)
continue % ask for the height again
end
break % break (i.e., exit the inner while-loop) when a is in the valid range
end
repeat = input('Do you want to change it? (yes/no)','s');
end
2 Comments
Voss
on 25 Mar 2022
You're welcome!
Be sure to test it and make sure it does what you're trying to do!
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!