I need some help with a mathlab project

2 views (last 30 days)
Edgar Hernandez Recio
Edgar Hernandez Recio on 26 Nov 2021
Answered: DGM on 26 Nov 2021
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

Answers (1)

DGM
DGM on 26 Nov 2021
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

Tags

Community Treasure Hunt

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

Start Hunting!