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

bookRelational Operators

メニューを表示するにはスワイプしてください

Relational operators (>, <, >=, <=, ==, !=) are used to compare two values. They determine the relationship between the values 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

main.cpp

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.

first_boolaplha_usage.cpp

first_boolaplha_usage.cpp

second_boolaplha_usage.cpp

second_boolaplha_usage.cpp

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. The not equal operator (!=) is useful when you want to perform an action only if two values are different.

main.cpp

main.cpp

copy
123456789101112
#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; 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

main.cpp

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.

main.cpp

main.cpp

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?

question mark

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

正しい答えを選んでください

question mark

How can you check if two strings are not equal?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  8

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  8
some-alt