2

Basically I have two while loops in my code. All they do is count in increments of 1 from 0 to either 10 or -10. The vectors that count are "count1" and "count2". This is happening in my code in two separate while loops. However, I now need each count dependent on each other so I need them within the same loop. Is it possible to have the two separate while conditions working in one loop. For example, what I have now is:

  count1 = 0
  count2 = 0
  l_RWM = vector()
  r_RWM = vector()      


while (count1 < 10 && count1 > -10){

  count1 = count1 + (sample(c(1,-1), 1, prob = c(.55,.45)))

  l_RWM = append(l_RWM,count1)

}

while (count2 < 10 && count2 > -10){

  count2 = count2 + (sample(c(1,-1), 1, prob = c(.55,.45)))

  r_RWM = append(r_RWM,count2)

}

But I want something like

while (count1&count2 < 10 && count1&count2 > -10){

if(count1 < 10 && count1 > -10) count1 = count1 + (sample(c(1,-1), 1, prob = c(.55,.45)))
    else count1 = count1

  if(count2 < 10 && count2 > -10) count2 = count2 + (sample(c(1,-1), 1, prob = c(.55,.45)))
    else count2 = count2

    l_RWM = append(l_RWM,count1)
    r_RWM = append(r_RWM,count2)

 }

My "if" code is supposed to only have the count occur when the object hasn't reached 10 or -10. E.G. count2 will still count up or down even when count1 has finished. My code doesn't work and I am not looking for an answer but more so a reason to why it doesn't. Keeping in mind that I am very new to R and I apologise in advance if this question is trivial :p.

For anyone wondering, the reason I need them in the same loop is because I'm looking to have something like: if count1 increases than count2 decreases.

Thanks

Gabriel
  • 55
  • 1
  • 1
  • 5
  • Your `if` statements are not necessary because they will always be `TRUE` inside your `while` loop. – Señor O Jan 23 '14 at 01:31
  • @SeñorO What if count1 reaches 10 or -10, and count2 has not. Does that give them a use? Or am I seeing this wrong. Thanks for the reply. – Gabriel Jan 23 '14 at 01:37
  • You want to use `||` not `&&` – smac89 Jan 23 '14 at 01:37
  • @Gabriel if count1 reaches 10 or -10, the statement (count1 & count2 < 10 && count1 & count2 > -10) will be `FALSE` – Señor O Jan 23 '14 at 01:38
  • @SeñorO I see. That is why my loop is terminating when one count finishes. So my issue is within my while condition. – Gabriel Jan 23 '14 at 01:54

1 Answers1

3

I am assuming, from your pseudo code, that you are just wanting to make sure both count1 and count2 are less than 10 and that count1 and count2 are greater than -10. What I would do is something like this:

while (count1 < 10 && count2 < 10 && count1 > -10 && count2 > -10){

You shouldn't need to group anything with parenthesis since everything is ANDed together and there is no mixed logic.

snypeNET
  • 86
  • 1
  • 3
  • This code works better, as the loop actually counts now. However, it terminates as soon as one of the counts reaches 10 or -10. – Gabriel Jan 23 '14 at 01:46
  • If you want it to keep going tillboth reach either 10 or -10 then use OR. while((count1 < 10 && count2 < 10) || (count1 > -10 && count2 > -10)) this way once one of the counts reaches 10 or -10 the condition will still be true till the other count reaches 10 or -10. – snypeNET Jan 31 '14 at 03:16