Jump to content

C++ Boolean s

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
b3hr0uz

b3hr0uz

    Newbie

  • Members
  • Pip
  • 2 posts
im doing a research on boolean.

The Boolean data type uses an integer storage code. How is this useful to a
programmer?

#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I'm not sure I understand your question. A boolean data type is a good choice if you want to store a variable that can only have 2 values (true or false).

#3
b3hr0uz

b3hr0uz

    Newbie

  • Members
  • Pip
  • 2 posts

brownhead said:

I'm not sure I understand your question. A boolean data type is a good choice if you want to store a variable that can only have 2 values (true or false).


what i mean is,
why do programmers use a boolean

#4
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Well its for the sake of simplicity and readability of your code. Using a boolean would be the same as using an integer, however, using an integer is less concise.
Perhaps an example will help me explain this better

Lets say we have the function isReady, that returns an integer. How does someone know what return value means true (it is ready) or false (it is not ready). It could be 1 means true, or 0 means true, or 1992 means true. Its a bit confusing unless your the one who wrote the function. So this would be a case in which you would use boolean values, solving the confusion.

This is just one way that boolean values may be used, but I think its a decent example.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
In C, the boolean type didn't exist. As a result, integer values were used, with 0=false, other=true. For converting the other way, false=0, true=1. Unfortunately, the result was that different people would create aliases for TRUE and FALSE using macros. They varied from programmer to programmer, with various cases being used.

C++ maintains the implicit conversion rules from C programming, and simply standardized true and false as the values a boolean could take.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
TrakStar

TrakStar

    Newbie

  • Members
  • Pip
  • 4 posts
Good answer