For some people it may be difficult to understand what Boolean is.Well, it's a type of algebra.

We will be learning about three Boolean operators today-AND,OR,NOT

The order for evaluating Boolean operators are as follows:-

AND-If you have a code that should be executed if two conditions are true, you use the and operator.And is shown as && in C++.1 && 0 will evaluate to 0 because one of them is false.

OR-Same as and, but either or both of the conditions must be true.Or is shown as || in C++.1 || 0 will evaluate to 1 since one of them is true.

NOT-Exactly reverses a statement.It is shown as ! in C++.!(1 && 0) will evaluate to 1.Why you ask?1 && 0 evaluates to 0 and !0 (reversed) will become 1.


Now lets try out a complicated Boolean statement-

!( (1 || 0) && 0 )ANSWER=1.(Brackets are always first)

1 || 0 = 1
1 && 0 = 0
!0 (reversed) = 1


Finally,why are Boolean operators used?Well,to make more complex conditional statements.Suppose you want a number to be less than 10 and more than 5 you use &&.


Thanks for checking out this tutorial,
cadence441

Recommended Answers

All 2 Replies

If you are doubtful,do not hesitate to ask.

Didn't you think it was important to mention that C++ also implements the AND and OR operators as short-cut operators, that is, if evaluating the first expression is sufficient to get the result of the boolean operation, then the other operands are not computed.

For example, here's a little question:

#include <iostream>

int main() {
  int i = 0;
  bool b = i++ && ++i;
  std::cout << "the value of 'i' is: " << i << std::endl;

  i = 0;
  b = ++i && i++;
  std::cout << "the value of 'i' is: " << i << std::endl;

  i = 0;
  b = i++ || ++i;
  std::cout << "the value of 'i' is: " << i << std::endl;

  i = 0;
  b = ++i || i++;
  std::cout << "the value of 'i' is: " << i << std::endl;

  return 0;
};

What's the output?

(BTW, that's actually a pretty good exam question in a basic C/C++ course)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.