The bool Class

Page last updated 04/21/99



 
 

Why the need for bool?

Conditions (part of the if statement) are integral to most software.  Conditions are integral to other control structures that we will study later.

Conditions always compute a value of true or false. The bool class uses the C/C++ logical operations and allow you to create bool objects to hold values of true and false.

Bool objects can be used to simplify complex conditions and/or manipulate logic values more directly.
 
 
 
 
 
 
 
 
 
 


 
 
 


Example bool Objects

A variation on the overtime computation:
 
The straightforward approach Using a bool variable
...

if(hours>=40){

    hours = hours + (40-hours)*0.5

}
bool overtime;

...

overtime = hours >= 40;

...

if(overtime){

   ....

}

From page 248...

// Demonstrate a simple bool function

#include <iostream>

using namespace std;



bool odd(int n)

{ // post: Return true if n is an odd integer

  return (n % 2) != 0;

}



int main()

{

  int j = 3;



  // Ensure j is an even number

  if(odd(j))

  {

    j = j + 1;

  }

  cout << j << endl;



  return 0;

}

 

 
 
 
 
 


 


Boolean Operations

Conditions, logical values can be combined using the operations and, or, and not.
 
and

&&
gpa>=0.0 && gpa <=4.0

0.0<=gpa && gpa <=4.0
or

||
grade=="A" || grade=="A-"
not

!
!overtime
T && T --> T

T && F --> F

F && T --> F

F && F --> F
T || T --> T

T || F --> T

F || T --> T

F || F --> F
! T --> F

! F --> T

Consider when


 
 
 
 


 


Precedence of C++ operators

Category Operators Descriptions Associativity
Parenthesis
( )  [ ]
Parenthesis and array access left to right
Unary
!   -
Not and negation right to left
Multiplicative
* / %
Multiplication, Division and Remainder left to right
Additive
+ -
Plus and minus left to right
Input/Output (Shift)
<< >>
Stream insertion and extraction left to right
Relational
< <= > >=
Less than, etc. left to right (should never connect these together)
Equality
== !=
Equal and not equal left to right (should never connect these together)
And
&&
Logical and left to right
Or
||
Logical or left to right
Assignment
=
Assignment right to left

 


 
 

Precedence Examples

What bool values do these expression evaluate to? Write a bool expression that is true if the value of number is outside the range of 0 to 50.
 
 
 
 


 
 

Revisit to the Grid class

// Show a more complex logical expression inside a bool function

#include <iostream> // For cout

using namespace std;

#include "grid.h" // For the grid class



bool moverOnEdge(const grid & g)

{ // post: Return true if the mover is on an edge or false otherwise

  bool result = false;



  result =    (g.row() == 0)                  // On north edge?

           || (g.row() == g.nRows()-1)        // On south edge?

           || (g.column() == 0)               // On west edge?

           || (g.column() == g.nColumns()-1); // On east edge?



  return result;

}



int main()

{ // Test drive moverOnEdge

  grid tarpit(6, 6, 2, 5, east);

  if(moverOnEdge(tarpit))

  {

    cout << "On edge" << endl;

  }

  else

  {

    cout << "Not" << endl;

  }

  

  return 0;

}

 

 
 
 
 
 


 
 

Short Circuit Evaluation

When you use && and ||, the computer will only evaluate enough of the logical expression to conclude the final result.

If the left side of && is false then the right side can be ignored because the answer will be false.

If the left side of && is true then the right side actually must be evaluated-- the right side determines the answer.

If the left side of || is true then the right side can be ignored-- the answer will be tru.

If the left side of || is false then the right side determines the answer.
 
 
 
What you write... How the computer short circuits it
if(gpa>=0.0 && gpa <=4.0){

   cout << gpa;

}
if(gpa>=0.0){

    if(gpa<4.0){

       cout << gpa;

    }

}

 
 
 


 
 

bankAccount Example

bool bankAccount::withdraw(double amount)

{

   bool result = true; //assume a valid transaction



   if(amount >= my_balance || amount < 0){

      result = false;

   } else {

      my_balance = my_balance - amount;

   }



   return result;

}
This function is then called in the context of an if...
...

    bankAccount ba("test it",50.00);

...

    cout << "Enter amount of withdraw";

    cin >> amt;

    if(ba.withdraw(amt)){

       cout << "Success. Balance = " << ba.balance() << endl;

    } else {

       cout << "Transaction failed. Insufficient funds." << endl;

    }
 

 
 
 
 
 
 
 


 
 

Programming Tips

Be very careful between the use of = and ==.

Use the { } compound statement even if there's just a single statement

The way you write something in algebra is not necessarily the way you can write it in C++. (0.0<=gpa<=4.0) must be written as we did above.

Short circuit evaluation will have some importance later.
 
 
 
 


 
 

LeapYear Example

Write a bool function that determines if the year passed as an integer argument is a leap year or not.

The rules are: If the year is divisible by 4, it's a leap year, unless it is divisible by 100 then it is not, unless it is divisible by 400 then it is leap year.  1900 was not a leap year, but 2000 will be a leap year (another potential Y2K glitch programmers were worried about)