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

Réponses (1)

DGM
DGM le 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

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by