How to stop a loop when the wrong values are entered

조회 수: 1 (최근 30일)
Jonathan Hall
Jonathan Hall 2016년 3월 22일
댓글: Jonathan Hall 2016년 3월 22일
Hey everyone,
Fairly new to Matlab and I'm having some troubles with looping. For my assignment I need to create a program that will end if an unacceptable value is added (i.e. a negative number or an angle outside of the given range.) I also need it to display a message stating that a wrong value was added. Here's what I have:
H=input('Enter a value greater than 0 for height in feet ');%Height in ft
L=input('Enter a value greater than 0 for length in feet ');%Length in ft
W=input('Enter a positive value for work in pounds force ');%Work in lbf
aW=input('Enter the angle of alpha W between 0 and 360 degrees ');%Alpha W in degrees
while true
if H<0 || L<0
break
end
if aW<0 || aW>360
break
end
end
Wx=W*cosd(aW) Wy=W*sind(aW)
The book I'm using isn't too clear on how to use loops but I think I need to use the while loop. No matter what values I enter, I can't get a message to display (I know that I don't have one in this program, I deleted it because it didn't work) and instead of stopping it will end the loop and proceed to calculate Wx and Wy. Any help would be greatly appreciated! If I left anything out let me know. Thanks!
-Jon
  댓글 수: 3
Jonathan Hall
Jonathan Hall 2016년 3월 22일
I had an if statement after the loop, but that didn't do what I needed to be done
Image Analyst
Image Analyst 2016년 3월 22일
Very simple solution. Step through it with the debugger.

댓글을 달려면 로그인하십시오.

채택된 답변

Rick Rosson
Rick Rosson 2016년 3월 22일
편집: Rick Rosson 2016년 3월 22일
% Height in feet:
H = 0;
while H <= 0
H = input('Enter a value greater than 0 for height in feet ');
end
  댓글 수: 1
Jonathan Hall
Jonathan Hall 2016년 3월 22일
Fantastic! That helps a lot. Of course it's something so simple haha. Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile 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