Comparison Operations
// This program will have the user to enter two numbers
// and the program will compare them using the existing
// comparison operations in C++.
// These are the comparison operators:
// > greater than
// < less than
// == equal to
// >= greater than or equal to
// <= less than or equal to
// != not equal to
// Syntax: op1 COP op2
// where: op1 and op2 are of the same data type.
// COP is the comparison operator
// returns: boolean
#include<iostream>
using namespace std;
int main()
{
int num1 = 0;
int num2 = 0;
// Note: another way to declare and initialize above
// variables is:
// double num1 = 0, num2 = 0;
// This is possible since both variables have the same
// data type, that's why int only appears once and each
// variable and initialization is separated by commas,
// then whole statement is terminated by a semicolon.
cout << "Enter the first integer: ";
cin >> num1;
cout << "Enter the second integer: ";
cin >> num2;
cout << endl;
// This cout will display the two numbers entered
// by the user, and then display the comparison value.
// On this statement, it is checking if num1 is greater than
// num2. This will display a boolean value of 1 (true) or 0 (false).
// Note: If doing these operations within cout, better enclose
// them within parenthesis. This will ensure that the computation
// will execute within parenthesis and the value returned by
// the computation will be the only thing handled by the
// cout.
cout << num1 << " > " << num2
<< ": " << (num1 > num2) << endl;
cout << num1 << " < " << num2
<< ": " << (num1 < num2) << endl;
cout << num1 << " == " << num2
<< ": " << (num1 == num2) << endl;
cout << num1 << " >= " << num2
<< ": " << (num1 >= num2) << endl;
cout << num1 << " <= " << num2
<< ": " << (num1 <= num2) << endl;
cout << num1 << " != " << num2
<< ": " << (num1 != num2) << endl;
// If you want to display the actual true or false words
// you have to modify the comparison operation with a
// one-liner if-statement.
// syntax: condition ? value_if_true : value_if_false
// where
// condition: the actual comparison operation
// value_if_true: the value to be return by this
// if-statement when condition returns true.
// value_if_false: the value to be returned by
// this if-statement when condition returns false.
// usage: Instead of the normal (op1 COP op2), this will be
// the syntax:
// cout << num1 << " > " << num2
// << ": " << ((num1 > num2) ? "true" : "false") << endl;
return 0;
}