Relational Operators
Relational operators (>
, <
, >=
, <=
, ==
, !=
) are used to compare two values. They determine the relationship between the values and and give true
or false
.
To output true
when printing a boolean value with std::cout
, you can simply use std::cout
with a bool value.
main.cpp
123456#include <iostream> int main() { std::cout << true; }
By default, std::cout
prints one for true
and zero for false
. To print true
and false
as words, you need to use the std::boolalpha
manipulator. It instructs std::cout
to display boolean values as words instead of numbers.
first_boolaplha_usage.cpp
second_boolaplha_usage.cpp
123456#include <iostream> int main() { std::cout << std::boolalpha << true; }
Using operators
To compare whether something is equal, use the ==
operator with two equal signs. Remember, =
is a different operator used for assignment, not comparison.
main.cpp
123456789#include <iostream> int main() { // Imagine you need to verify if the user has entered the correct password std::cout << std::boolalpha; std::cout << ("yw>r'Peq/2d" == "yw>r'Peq/2d") << std::endl; std::cout << ("yw>r'Peq/2d" == "VzF>.6Qy(UI?") << std::endl; }
When using the >
(greater than) and <
(less than) relational operators, you can compare two values to check which one is larger or smaller. The result will be true
if the condition holds, and false
otherwise.
main.cpp
123456789#include <iostream> int main() { std::cout << std::boolalpha; // Checking if a customer has enough balance // To withdraw 300 from an account balance of 500 std::cout << (500 > 300) << std::endl; }
If the user wants to withdraw 500 and their balance is also 500, the >
operator will return false
, as it checks if the balance is strictly greater than the withdrawal amount. However, the withdrawal is still possible. In this case, you should use the >=
or <=
operator to check if the balance is greater than or equal to the withdrawal amount, which would correctly return true
.
main.cpp
123456789#include <iostream> int main() { std::cout << std::boolalpha; // Checking if a customer has enough balance // To withdraw 500 from an account balance of 500 std::cout << (500 >= 500) << std::endl; }
1. What is the default behavior of std::cout
when printing a boolean value?
2. How can you check if two strings are not equal?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Relational Operators
Swipe to show menu
Relational operators (>
, <
, >=
, <=
, ==
, !=
) are used to compare two values. They determine the relationship between the values and and give true
or false
.
To output true
when printing a boolean value with std::cout
, you can simply use std::cout
with a bool value.
main.cpp
123456#include <iostream> int main() { std::cout << true; }
By default, std::cout
prints one for true
and zero for false
. To print true
and false
as words, you need to use the std::boolalpha
manipulator. It instructs std::cout
to display boolean values as words instead of numbers.
first_boolaplha_usage.cpp
second_boolaplha_usage.cpp
123456#include <iostream> int main() { std::cout << std::boolalpha << true; }
Using operators
To compare whether something is equal, use the ==
operator with two equal signs. Remember, =
is a different operator used for assignment, not comparison.
main.cpp
123456789#include <iostream> int main() { // Imagine you need to verify if the user has entered the correct password std::cout << std::boolalpha; std::cout << ("yw>r'Peq/2d" == "yw>r'Peq/2d") << std::endl; std::cout << ("yw>r'Peq/2d" == "VzF>.6Qy(UI?") << std::endl; }
When using the >
(greater than) and <
(less than) relational operators, you can compare two values to check which one is larger or smaller. The result will be true
if the condition holds, and false
otherwise.
main.cpp
123456789#include <iostream> int main() { std::cout << std::boolalpha; // Checking if a customer has enough balance // To withdraw 300 from an account balance of 500 std::cout << (500 > 300) << std::endl; }
If the user wants to withdraw 500 and their balance is also 500, the >
operator will return false
, as it checks if the balance is strictly greater than the withdrawal amount. However, the withdrawal is still possible. In this case, you should use the >=
or <=
operator to check if the balance is greater than or equal to the withdrawal amount, which would correctly return true
.
main.cpp
123456789#include <iostream> int main() { std::cout << std::boolalpha; // Checking if a customer has enough balance // To withdraw 500 from an account balance of 500 std::cout << (500 >= 500) << std::endl; }
1. What is the default behavior of std::cout
when printing a boolean value?
2. How can you check if two strings are not equal?
Thanks for your feedback!