C++ Programming Series: Complex Conditions

In the previous post, the topic of ‘Using Conditional Statements’ finally, came to an end. But only now, there is something we call as ‘complex’. Actually, from the word, ‘complex’, it means multiple. In fact, complex is always something, multiple. A single complex object is a combination of multiple simple objects.

A simple example of a complex condition is given below:

bool complexOr = 5 < 3 || 2 < 8;</pre>

The whole expression after the assignment operator(or simply, = i.e equals to) gives either true or false and then, stores it in the boolean variable called as complexOr. 5 < 3 is understandable and is a simple condition when used alone. Same is the case for 2 < 8. First one is false and the second one is true. But the new thing is the operator in between these two simple conditions which connects them, forming a complex condition.

This is the OR logical operator(||) and it connects two or more conditions in such a way that:

If anyone of the conditions are true, it will return true.

For the above case, the boolean variable, complexOr will hold true. This is because 2 < 8 is true.

There is another operator of which example is given below:

bool complexAnd = 5 < 3 && 2 < 8;

The two ampersands(&&) is called AND logical operator and it connects two or more conditions in such a way that:

If all of the conditions are true, it will return true.

Therefore, it is pretty, clear that complexAnd will hold false. This is because one of the conditions i.e 5 < 3, is false.

In the next post, we will talk more about complex conditions and calculations. Be easy with yourselves and don’t get panic by the amount of text. Be patient and practice programming on daily basis!

C++ Programming Series: Basic Arithmetic

We discussed many things till now. We discussed about printing the variables, primitive types, user input and some conditional statements. What we should discuss right now is the basic arithmetic.

It is very simple to do primitive calculations with primitive types. We will only work with integers here. But it will be applicable to all other primitive types (of course, ‘char’ is one of them).


int a = 10;
int b = a + 5;
cout << b << endl; //Prints 15

int c = 12;
c = c + 5;
cout << c << endl; //Prints 17

int d = a + c;
cout << d << endl; //Prints 27


int a = 10;
int b = a - 5;
cout << b << endl; //Prints 5

int c = 12;
c = c - 5;
cout << c << endl; //Prints 7

int d = a - c;
cout << d << endl; //Prints 3


int a = 10;
int b = a * 5;
cout << b << endl; //Prints 50

int c = 12;
c = c * 5;
cout << c << endl; //Prints 60

int d = a * c;
cout << d << endl; //Prints 600


int a = 10;
int b = a / 5;
cout << b << endl; //Prints 2

int c = 12;
c = c / 5;
cout << c << endl; //Prints 2
//For integers and characters, remainder is discarded, it is the quotient which is left.
//For floats and doubles, there is no remainder and precise division is done.
//For the above division, it will be 2.4 for floats and doubles.

int d = a / c;
cout << d << endl; //Prints 5

I hope that you might have understood all primitive calculations. One can understand it easily if he/she knows algebra. I will not tell you what is algebra because it is not the purpose of this place. Just Google it if you don’t know. However, there are some short hand methods to some of them, which are really useful. Following are those methods:

int add = 10;
add += 5; //equivalent to "add = add + 5;"

cout << add << endl; //Prints 15

int sub = 10;
sub -= 5; //equivalent to "sub = sub - 5;"

cout << sub << endl; //Prints 5

int mult = 10;
mult *= 5; //equivalent to "mult = mult * 5;"

cout << mult << endl; //Prints 50

int divi = 10;
divi /= 5; //equivalent to "divi = divi / 5;"

cout << divi << endl; //Prints 2

There are two more methods that are very confusing but produces neatness in one’s coding style.

int a = 10;
a++; //equivalent to "a = a + 1;" but with some exceptions
++a; //equivalent to "a = a + 1;" but with some exceptions

cout << a << endl; //Prints 12
//Similar is for subtraction

The first one (a++) is called as postfix increment operator and the other one (++a) is called as prefix increment operator. They are not the same and are very much confused with each other. Similar is the case for postfix and prefix decrement operators (i.e, a--, --a). This can be proved by the code below:

int print = 25;

//Method 1.
//cout << print = print + 1 << endl; //This gives error and so, is not possible.

//Method 2.
cout << print++ << endl; //Prints 25

//Method 3.
cout << ++print << endl; //Prints 27

Awww… What’s that? It’s a strange looking code! But let me explain all this. Both postfix and prefix increment and decrement methods can be placed with variables in a complete C++ code sentence as well. Same is not true for a = a + 1; or a = a - 1;. In the 2nd method, print is first printed out and then, incremented by 1. So, it prints 25 and then, its value is changed to 26 i.e print = print + 1; is done after being printed. In the 3rd method, print is first incremented by 1 and then, is printed out i.e print = print + 1; is done before being printed (Remember, 2nd method already changed its value to 26 and so, in 3rd method, 27 is printed out). The equivalent code to this is given below:

int print = 25;

//"cout << print++ << endl;" is equivalent to
cout << print << endl; //Prints 25
print = print + 1;

//"cout << ++print << endl;" is equivalent to
print = print + 1;
cout << print << endl; //Prints 27

There is another thing. We can have remainder instead of quotient, in dividing numbers by a special operator called as modulo operator, represented by %.

int a = 14;
int b = 5;
cout << a / b << endl; //Prints 2, we can do temporary calculations and print them.
cout << a % b << endl; //Prints 4 which is the remainder obtained by dividing a by b.

a %= a; //a is now, 0.
b %= 3; //b is now, 2.

This post is too lengthy to explain all basic arithmetics in C++. But length don’t matters! It is your motivation and inspiration that matters!

C++ Programming Series: Using Conditional Statements (Part 1)

Conditional statements in a program are such statements which controls the progression of our program. We must understand the fact that our code is always executed from the start to the end linearly.

Following is a conditional statement:

#include <iostream>
using namespace std;
int main()
 int a = 5;
 if(a < 10)
  cout << "a is smaller." << endl;
 return 0;

Here, if shows the condition that is typed after it in round brackets. In the curly brackets or the block just after the if statement, there is the code which will be executed only if the condition is true.

> (bigger than), >= (bigger than or equal to), == (equals to). The last one i.e == is quite mismatched with =. == checks the equality between two things whereas = copies the value of the right hand side object to the left hand side object.

It is not necessary to use a block below the if-statement. If there is only one line of code, then just have it. This is true for all kinds of conditional statements. For example, for the above if statement, you can also type:

if(a < 10)
 cout << "a is smaller." << endl;

You may have noticed the spaces that are left at the start of a statement in a block or after the conditional statements. This is called formatting. We need to format the code so that it is more readable. The spaces or tabs indicates that in which block a particular line of code is present. Of course, it will be helpful in future when we make some medium size projects.

Now, lets see a better conditional statement. Instead of the previous if-statement, we can also type in:

if(a < 10)
 cout << "a is smaller." << endl;
 cout << "a is bigger." << endl;

With a equals to 5, we can see no change in the output. But if a is, lets say, 15, it will execute the line of code below the else. You can read it as:

If a is smaller than 10, print “a is smaller” else print “a is bigger”.

The else-statement is always typed with an if-statement. Lets type in another if-statement.

if(a < 10)
 cout << "a is smaller." << endl;
if(!(a < 10))
 cout << "a is bigger." << endl;

The ! (or NOT) operator is the opposite of the conditional state. For example, if a is not smaller than 10, it is false. But putting NOT operator at the back of it makes it true. To check whether two things are not equal to each other, we use != which is another comparing operator.

For the else-statement that we typed, its condition is quite similar to !(a < 10). Now, you may have understood the else-statement.

Don’t try to understand it or go deep in the code; just type the code and make it run!