Home

C++ Keywords: bool

 

Introduction

The Boolean data type is used to declare a variable whose value will be set as true (1) or false (0). To declare such a value, you use the bool keyword. The variable can then be initialized with the starting value. A Boolean constant is used to check the state of a variable, an expression, or a function, as true or false.

You can declare a Boolean a variable as:

bool GotThePassingGrade = true;

Later in the program, for a student who got a failing grade, you can assign the other value, like this

GotThePassingGrade = false;

Here is an example:

#include <iostream>
using namespace std;
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
    bool MachineIsWorking = true;

    cout << "Since this machine is working, its value is "
         << MachineIsWorking << endl;

    MachineIsWorking = false;

    cout << "The machine has stopped operating. "
         << "Now its value is " << MachineIsWorking << endl;

    return 0;
}

//---------------------------------------------------------------------------
   
 
 

Copyright © 2002-2009 FunctionX, Inc.