Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if statement integer

I think this is a simple question, but I'm struggling with the following. In my example I have the following statement (language is C):

int foobar  if (foobar) { // do something. } 

Now, if I am correct about this, this statement is true when foobar is not zero. So it should be much the same as if (foobar!=0).

But what happens if foobar becomes a negative number?

like image 241
Bart Teunissen Avatar asked Feb 01 '13 12:02

Bart Teunissen


People also ask

Can we use int in if condition?

The reason you get cannot convert an int to boolean is that Java expects a boolean in the if(...) construct - but menuNumber is an int . The expression menuNumber == 1 returns a boolean, which is what is needed. It's a common mix-up in various languages.

How do you check if a number is an integer?

To check if a String contains digit character which represent an integer, you can use Integer. parseInt() . To check if a double contains a value which can be an integer, you can use Math. floor() or Math.

What is an example of an if statement?

if (score >= 90) grade = 'A'; The following example displays Number is positive if the value of number is greater than or equal to 0 . If the value of number is less than 0 , it displays Number is negative .


1 Answers

negative or positive. Anything that's not a 0 is a true value in if

Also, Consider a negative number: -1

-1 in C internally is represented as: 0xFFFFFFFF, in which case, it would be a positive number if I cast it to unsigned integer.

But after the advent of C99 standard compilers, I suggest you use <stdbool.h> instead. Makes the guessing work a lot less:

Read here about stdbool.h

like image 109
Aniket Inge Avatar answered Sep 19 '22 09:09

Aniket Inge