Kapil Lamichhane
Kapil Lamichhane

Reputation: 169

why is this character matching not working in r?

apple_orange<-readline(prompt="Enter your fruit: ")
  while((apple_orange!="apple")||(apple_orange!="orange"))
  {
    apple_orange<-readline(prompt="Enter your fruit: ")
  }

It keeps on Looping even when I type apple or orange .. What am I doing wrong here?

Upvotes: 2

Views: 106

Answers (1)

eipi10
eipi10

Reputation: 93761

Your condition says to keep the loop going if apple_orange != "apple" OR apple_orange != "orange". One of those conditions will always be true, making the overall condition true, so the loop keeps going. Just change the OR to an AND.

Upvotes: 1

Related Questions