I need some help with a mathlab project

Hi, I am making a mathlab project and I can't find any information about hoy can I code the next thing
x=0;
I want to create a loop doing every time x=x+1 ,until I write in the command window
  • 'A' : instead of x=x+1 , do x=x+2;
  • 'B': instead of x=x+1,do x=x+3
  • 'E ' : exit the loop
It is like a loop with 3 states , the state A adding 2 to the counter, the state B adding 3 to the counter, the state C adding 1 to the counter and finally the state E exiting the loop.
I want the loop constantly working, and at the same time while the loop is working I want to be able to write the state in the command window. If I write nothing the loop is still working.
thanks

回答(1 个)

Use a while loop. Switch-case is more concise.
x = 0;
while true
fprintf('x is currently %d \n',x)
uresp = input('blah blah blah type something: ','s');
switch lower(uresp) % does case-sensitivity matter?
case 'a'
% do a different thing
case 'b'
% do a different thing
case 'e'
break;
otherwise
% do the normal thing
end
end

类别

帮助中心File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by