lemonbilly
lemonbilly

Reputation: 13

multiple conditions (or) in while loop - java

My while loop looks like this currently:

    while((0 <= trader.getWallet()) || (0 <= days)) {  
// do something
}

so if any of those two conditions are false somewhere in the loop the program will exit, but my program doesn't seem to listen to the second condition only the first, but it would listen to them separately if I just put them in one at a time for testing, so did I write this while loop wrong?

Upvotes: 1

Views: 267

Answers (2)

Fariya Rahmat
Fariya Rahmat

Reputation: 3220

In case of Logical OR if any one of the two conditions is true whole statement shall execute to be true. In case of Logical AND both the conditions have to be true for the whole statement to be true. So ,you can use logical AND

while((trader.getWallet>=0()) && (days>=0))

Upvotes: 3

Premkumar chalmeti
Premkumar chalmeti

Reputation: 1018

If the first condition is true then OR won't evaluate the second condition,

Instead, it should be

while(trader.getWallet() > 0 && days > 0) {  
   // do something
}

Upvotes: 1

Related Questions