Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Relational Operators | Introduction to Operators
C++ Introduction
course content

Course Content

C++ Introduction

C++ Introduction

1. Getting Started
2. Introduction to Operators
3. Variables and Data Types
4. Introduction to Program Flow
5. Introduction to Functions

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?

Select the correct answer

How can you check if two strings are not equal?

How can you check if two strings are not equal?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt