Data Types
Boolean Logic
Boolean values store true or false and help choose branches.
Boolean Logic
bool_logic.cpp
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
#include <iostream>
#include <string>
int main() {
int age = ;
bool hasPass = ;
bool canEnter = age >= 18 && hasPass;
std::string result = "";
if (canEnter) {
result = "enter";
} else {
result = "wait";
}
std::cout << "age=" << age << std::endl;
std::cout << "hasPass=" << hasPass << std::endl;
std::cout << "result=" << result << std::endl;
return 0;
}
logical and
The `&&` operator is true only when both sides are true.