0

I have a relatively simple issue using while loop. I am not sure what I am missing. I have an equation that computes r and I want to check whether or not it lies in some range. The following is what I have (I am just writing the main part):

  r = r + 0.2*dt*randn;
  r_temp = r;
  while r_temp<1.28 && r_temp>1.45
      r = r + 0.2*dt*randn;
      r_temp = r;
      if r_temp>1.28 && r_temp<1.45
          break
      end
  end

Basically, I need the above while loop to continue until r is inside 1.28<r<1.45. Can someone please advise as to what I may be missing?

Thanks!

2 Answers 2

1

You are searching for "Or"-statement, which is || to be on the right way. Edit: Too slow :(

0
0

while r_temp<1.28 && r_temp>1.45 continues while r_temp is both less than 1.28 and greater than 1.45. That can't happen. You want ||, not &&.

1
  • @user2468702: Did you change both the while and the if? You should only change the while. Apr 7, 2014 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.