cook
cook

Reputation: 13

how to use a unsigned int as a normal int in a if stament

Is there any way to use two unsigned ints in an if statement a a signed int?

unsigned int a = 0, b = 1;
if (a - b > - 1)
    ++a;

Upvotes: 1

Views: 129

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477358

How about:

if (a + 1 > b) {
  ++a;
}

(You may need to check for and deal wrap-around, i.e. the case where a + 1 == 0, if your values are completely unconstrained.)

Or even (thanks to @M.M.):

if (a >= b) {
  ++a;
}

Upvotes: 3

Related Questions