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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s