C++ Programming Series: Precedence, Order and sizeof()

In the previous post, we learned complex but still, simple looking conditions.

Instead of using boolean variables for complex conditions, we can directly use them in the conditional statements. Below is an example of a ‘complex looking’ complex condition:

int a = 5;
int b = 10;
int c = 15;

if( b == a + a || a == c - b && b != c - a && c > a )
{
  cout << "True!" << endl;
}
else
{
  cout << "False!" << endl;
}

Now, guess the output without executing it (or running it). It looks hard or complex to tell whether it is true or false. Actually, this will run in a specific order due to difference in the precedence. Precedence is the priority to check or run something in a statement. The precedence of AND logical operator(&&) is more than that of OR logical operator(||) which means that first, all AND logical operators will get executed and then, at last, the OR logical operator. The position do not justify the precedence; it is a property of an operator having a specific value.

Precedence helps to reduce errors. But it may be possible that programmer don’t want the order of execution in accordance with the precedence. The precedence factor can be overwritten with the use of round brackets.¬†Following code shows the order of running or executing code due to precedence, using round brackets:

if( (b == a + a || (a == c - b && b != c - a && c > a)) )
{
  cout << "True!" << endl;
}
else
{
  cout << "False!" << endl;
}

The inner round brackets run before the outer ones. If there is only a pair of round brackets, it will run before all other code. This is how, precedence decides it to run. And the output is, “True!”. Now, below is the overwritten precedence order using round brackets:

if( ( (b == a + a || a == c - b) && b != c - a) && c > a )
{
  cout << "True!" << endl;
}
else
{
  cout << "False!" << endl;
}

In the above code, the OR logical operator will be executed first. The change in precedence is clear as the output, this time, is “False!”.

Same is the case with the arithmetic operators i.e +, -, /, *, %, ++, –, and all other operators. Here is C++ Operator Precedence Table (There are many operators that we don’t know yet! It will be helpful in future as well.)

In the post of Primitive Types, we learned types which have different memories. Sometimes, using lesser memory is a good thing and optimizes the program. How can we know the amount of memory allocated (or used) by a type or a variable? The sizeof() operator is for this purpose.

float i_am_float = 10.99f;
double i_am_double = 99.101010;

//sizeof() will return the amount of memory allocated by the type or variable, mentioned in the round brackets.
cout << sizeof(long long int) << endl;
cout << sizeof(char) << endl;
cout << sizeof(i_am_float) << endl;
cout << sizeof(i_am_double) << endl;
cout << sizeof(double) << endl; 
cout << sizeof(string) << endl; //Non-primitive type. Remember, to include string

The memory allocated by the type is not universal for all platforms and IDEs (like Visual Studio).

We will see later on, how important precedence and sizeof() is, to our programming. But for now, just do as many experiments as possible with the precedence thing. Try 2+2-2/2*2 or something like that! Try 2+value++! Just keep trying until you reveal the precedence of the operators you know at that time. Then, get the output of the above sizeof() code.

Programming is something, boring before and interesting after learning!

Advertisements

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!