Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Relational Operators | Вступ до операторів
Вступ до C++
course content

Зміст курсу

Вступ до C++

Вступ до C++

1. Початок роботи
2. Вступ до операторів
3. Змінні та типи даних
4. Вступ до потоку програм
5. Вступ до функцій

bookRelational 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.

cpp

main

copy
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.

cpp

first_boolaplha_usage

cpp

second_boolaplha_usage

copy
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.

cpp

main

copy
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.

cpp

main

copy
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.

cpp

main

copy
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?
What is the default behavior of `std::cout` when printing a boolean value?

What is the default behavior of std::cout when printing a boolean value?

Виберіть правильну відповідь

How can you check if two strings are not equal?

How can you check if two strings are not equal?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 3
some-alt