C++ Operators and Operands

C++ <<:

There are two operators in C++ called the right-shift operator (>>) and left-shift operator (<<). They have different meanings depending on the context, because of operator overloading (their meaning changes based on their operands). In the context of cout, << means “put to,” according to Bjarne Stroustrup. It’s inspired by Unix operators like > and >>, which are used for writing to or appending to a file.

It’s a little confusing, and less straightforward than Java’s System.out.println() or even Python’s print(), but just know that you use << with cout and endl.

>> is associated with cin, which is input from the user rather than output to the console. cin is covered later in this section.

If a concept doesn’t make much sense to you, don’t worry. You’re not alone. Sometimes, the issue is poor language design. I’m really not a huge fan of C++, even though I have written some projects in it. But I’m including the basics in this book because it’s good to be aware of its existence, even if you end up mainly using JavaScript or Python or something instead.

C++ assignment operators:

#include <iostream>

#include <string>

using namespace std;

int main() {

string message = “Assignment operator demonstration”;

cout << message << endl;

int example = 1;

int anotherExample = 2;

example = anotherExample;

example -= 1;

example *= 2;

example /= example;

example += 4;

example %= 3;

cout << example << endl;

return 0;

}

C++ binary operators:

#include <iostream>

#include <string>

#include <cmath>

// <cmath> is used for pow() and floor()

using namespace std;

int main() {

string message = “Binary operator demonstration”;

// just kinda useless/random math to demonstrate operators

cout << message << endl;

int operatorDemo = 0;

operatorDemo = operatorDemo + 5;

int someOtherNum;

someOtherNum = operatorDemo – 1;

int someThirdNum = operatorDemo * someOtherNum;

// someThirdNum is 20 at this point

someThirdNum = someThirdNum / operatorDemo;

someOtherNum = someOtherNum % (operatorDemo – 1);

int powerToRaiseNumTo = 2;

int exponentDemo = pow(operatorDemo, powerToRaiseNumTo);

// floor division

// floor() just returns a number with no decimal place

cout << floor(5/2) << endl;

// == is used to check if something equals something else

// not to be confused with =, which is the assignment operator

cout << (5 == 2) << endl; //returns 0 to indicate false

cout << (1 == 1) << endl; //returns 1 to indicate true

// != means not equal to

cout << (3 != 2) << endl; //returns 1 because it’s true

cout << (4 > 7) << endl; //0 for false

int result = (4 < 7);

cout << result << endl;

bool trueFalse = (6 >= 5);

cout << “Value of trueFalse: ” << trueFalse << endl;

// you will notice that bools are 0 for false or 1 for true

// as opposed to other languages where it’s true and false

cout << (10 <= 10) << endl;

bool trueVar = 1;

bool falseVar = 0;

cout << (trueVar && falseVar) << endl; // 0 because AND requires both

cout << (trueVar || falseVar) << endl; //1 because || means or, only needs one to be true

return 0;

}

C++ unary operators:

Incrementing:

#include <iostream>

#include <string>

using namespace std;

int main() {

int incDemo = 31;

incDemo++; //post-increment

cout << incDemo << endl;

++incDemo; //pre-increment

cout << incDemo << endl;

return 0;

}

Decrementing:

#include <iostream>

#include <string>

using namespace std;

int main() {

int decDemo;

decDemo = 4;

cout << decDemo– << endl; // 4 because it does cout and then decrements

cout << decDemo << endl; //3

cout << –decDemo << endl; //2 because decrements first and then couts

return 0;

}

← Previous | Next →

C++ Topic List

Main Topic List

Leave a Reply

Your email address will not be published. Required fields are marked *