Sam
Sam

Reputation: 89

R - Only first condition in WHILE loop is being satisfied

I've simplified the following code from my actual project, since the issue is the same:

For some reason, when I run the following code:

x <- 1
y <- 5
z <- 20

while((x<y) && (z<28)){
  x <- x+1
  z <- z+1
}

paste(x, y, z)

My output is

"5, 5, 24"

Why would this WHILE loop stop running when variable z is still below 28?

Upvotes: 0

Views: 85

Answers (1)

Brian Balzar
Brian Balzar

Reputation: 307

The while loop stops when x hits the y value if you want it to continue change it from "x is less than y AND z is less than 28" to "x is less than y OR z is less than 28."

Upvotes: 1

Related Questions