JealousKnight
JealousKnight

Reputation: 43

How to shorten nested if statement

How can I shorten this nested if statement?

if(x > 0){
  if(grid[x-pixelOffset,y] == true){
    middleLeft = 1;
  }
}

Upvotes: 2

Views: 275

Answers (2)

James
James

Reputation: 21

Just for more information, if short-circuit is not desired, you may use the &. If you write if( (x>0) & grid[x,y]) {...}, the second part will be evaluated too.

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You can use the && operator:

if ((x > 0) && grid[x-pixelOffset,y]) 
    ...

You do not need == true when you check values of bool variables.

Upvotes: 5

Related Questions