In C++, boolean variables are declared using the bool data type. Here’s how to do it:
Declaration Syntax:
Basic Declaration:
bool isStudent; // Declares a boolean variableInitialization: You can also initialize a boolean variable at the time of declaration:
bool isStudent = true; // Initialized to true bool hasPassedExam = false; // Initialized to falseMultiple Declarations: You can declare and initialize multiple boolean variables in a single statement:
bool isStudent = true, hasPassedExam = false;
Example Code:
#include <iostream>
int main() {
bool isStudent = true; // Declaring and initializing
bool hasPassedExam = false; // Declaring and initializing
std::cout << "Is Student: " << std::boolalpha << isStudent << std::endl;
std::cout << "Passed Exam: " << hasPassedExam << std::endl;
return 0;
}
Output:
Is Student: true
Passed Exam: false
Summary:
Boolean variables in C++ are declared using the bool keyword, and they can be initialized to true or false. This allows for effective representation of binary states in your programs. If you have further questions or need clarification, feel free to ask!
