MATLAB how to make loop that user keeps pressing a negative number
This topic contains 2 replies, has 3 voices, and was last updated by Abhey 3 years, 8 months ago.
You must be logged in to reply to this topic.
Code with C | Programming: Projects & Source Codes › Forums › MATLAB › MATLAB how to make loop that user keeps pressing a negative number
This topic contains 2 replies, has 3 voices, and was last updated by Abhey 3 years, 8 months ago.
How do I keep looping so that if the user keeps pressing a negative number it keeps displaying the message “error”?
1 2 3 4 5 6 7 8 9 |
g=9.81 V0=input ('Enter initial velocity of projectile (m/s):') if V0<0 disp ('Error. Velocty must be greater than 0'); V0=input ('Enter initial velocity of projectile (m/s):') else x=input ('Enter target location in X direction (>0m):') theta=(asin(x*g/V0^2))/2 end |
you have to do is to give an initial value to V0 for instance: V0=-1
and then run the loop(put the whole code inside this loop)
If i keep inputting a negative answer it keeps repeating which i want
but if i run the program from the beginning and input a vale >0 for V0 it jsut falls through and gives an answer for theta without asking for the x value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
g=9.81 V0=0 V0=input ('Enter initial velocity of projectile (m/s):') while (V0<0) disp ('Error. Velocty must be greater than 0'); V0=input ('Enter initial velocity of projectile (m/s):') if (V0>0) x=input ('Enter target location in X direction (>0m):') theta=(asin(x*g/V0^2))/2; end end theta |
You must be logged in to reply to this topic.