bool v/s int flag

Nov 21, 2011 at 11:19am
i have to develop a program where i have to check for a condition and assign a variable a value true or false so i am confused between these two approach.
1 bool test = true
then according to case make it false or true,
2 int flag = 1
and then change it to 0 or 1;

which approach is better and why
Nov 21, 2011 at 11:51am
There are some differences to them.

Example:

with an int: 1 != -1;

with a bool: bool(1) == bool(-1);

And for me a bool is clearer to use in true or false cases cause it simply says what it is.
Nov 21, 2011 at 11:58am
The bool flag "false" is equal to 0. The bool flag "true" will return "1" when typed and anything that is not zero is equivalent to true if I am making sense.

You could easily use int, I see it done all of the time, however I prefer to use bool because it is a little clearer with what the label is used for.
Nov 21, 2011 at 12:00pm
Using bool helps other programmers understand your code (or even yourself a lot of time later).
When they see int they don't immediately know if you actually store a number or just use it as a flag.

Also: only 0 means false, anything other than 0 means true.
Last edited on Nov 21, 2011 at 12:01pm
Topic archived. No new replies allowed.