# Work with Boolean Operators

• 10-08-2009
Nean
Work with Boolean Operators
So I've been working a bit, with an Else If statement and some boolean operators. Even though I've managed to get it 100% working, I'm not sure if I did anything sloppy, or if there is a more effective way of doing it. If anyone can take a look over my code and let me know, I'd really appreciate it.

Code:

```#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) {  int age; //declaring our integer :O :O :O :O  char test; //Variable containing ONE character.         /* Comparisons evaluate out to a 0 or a 1 depending on whether     its true or false. 0 = false, and 1 = true.     Example: cout<< ( 2 == 1 ); evaluates to a 0     */         /* Not Operator:       Not operator accepts one input, if its true then it returns false       if its false it returns true. Not (any number != 0 = 0)       Is written as (!)     */         /* And operator:       Returns true if both inputs are true (1), otherwise returns false (0).       Written as &&. Evaluated before Or Operator.     */         /* Or Operator:       If either or borth of values are true, then it returns true.       Otherwise returning false (0). Written as ||. Evaluated after And     */           cout<<"Please input your age: "; // << for output     cin>> age; // >> for input     cin.ignore(); //Ignore enter.         if (!(age == 100)) { // (!100) { //start block       //If age < 100 then run the code!       cout<<"You're not 100  years old!!\n";       }     else if  (age == 100) {         cout<<"You're 100 years old!\n";         }             cin.get(); //Pause the program until enter is pressed. }```
• 10-08-2009
anon
The sloppy thing is that if you know that age is not different from 100, there's no reason to check again if it is 100.

In this situation it would also be much more common to use the != operator:

Code:

```    if (age != 100) { // (!100) { //start block       //If age < 100 then run the code!      //also, this comment is wrong!       cout<<"You're not 100  years old!!\n";       }     else {         cout<<"You're 100 years old!\n";         }```
• 10-08-2009
Nean
Quote:

Originally Posted by anon
The sloppy thing is that if you know that age is not different from 100, there's no reason to check again if it is 100.

In this situation it would also be much more common to use the != operator:

Code:

```    if (age != 100) { // (!100) { //start block       //If age < 100 then run the code!      //also, this comment is wrong!       cout<<"You're not 100  years old!!\n";       }     else {         cout<<"You're 100 years old!\n";         }```

Noted. Thanks a lot!
• 10-09-2009
RockyMarrone
Sloppy things are

Code:

```int age; //declaring our integer :O :O :O :O  char test; //Variable containing ONE character.```
should be initialized with a default value like

Code:

```#define SPACE ' ' int    age = 0;        //declaring our integer :O :O :O :O char test = SPACE; //Variable containing ONE character.```
the below code if i have to write i will write like this
Code:

```    if (!(age == 100)) { // (!100) { //start block       //If age < 100 then run the code!       cout<<"You're not 100  years old!!\n";       }     else if  (age == 100) {         cout<<"You're 100 years old!\n";         }```
like
Code:

```switch (age) {   case 100:     cout<<"You're 100 years old!\n";   break;   default:   cout<<"You're not 100  years old!!\n"; }```
This objdump and the footprint of above code will be faster than the if else statements :)
• 10-09-2009
laserlight
Quote:

Originally Posted by RockyMarrone
should be initialized with a default value like

I do not think that age should be initialised with a default value since it would be assigned a value very shortly thereafter. (This would have been more obvious if there were not those blocks of pedagogical comments in between.) test should not be initialised: it should be removed since it is never used.

Quote:

Originally Posted by RockyMarrone
This objdump and the footprint of above code will be faster than the if else statements

Maybe, if a compiler does not interfere and make the optimised code identical. Even if it does not do so, anon's suggestion of simplification to just an if and an else makes for more readable code since it expresses the intention better than a switch, and any performance gain will be so negligible that it would not matter either way.
• 10-09-2009
RockyMarrone
Quote:

Maybe, if a compiler does not interfere and make the optimised code identical. Even if it does not do so, anon's suggestion of simplification to just an if and an else makes for more readable code since it expresses the intention better than a switch, and any performance gain will be so negligible that it would not matter either way.
Any gain in the performance is very very huge it doesn't matter u r getting less or huge amount of performance gain......

If u will not think this way in embedded system programming then performance can't be improved
• 10-09-2009
laserlight
Quote:

Originally Posted by RockyMarrone
Any gain in the performance is very very huge it doesn't matter u r getting less or huge amount of performance gain

Then you are being penny-wise pound-foolish. If you can gain in performance at no or marginal cost to anything else, then you should do so (a canonical example being the use of standalone pre-increment instead of standalone post-increment for overloaded increment operators for class types). But here there may or may not be a gain in performance, and the gain is likely to be very small. It comes at the cost of readability/clarity: a reader would expect a switch to select between several values, but here it is used as a simple if-else.

Quote:

Originally Posted by RockyMarrone
If u will not think this way in embedded system programming then performance can't be improved

Are you so sure that Nean is doing embedded system programming? I daresay that it is obvious that Nean is just starting out, so he/she needs to learn how to write readable code, not learn hacks that may not matter. "It is far, far easier to make a correct program fast than it is to make a fast program correct."
• 10-09-2009
RockyMarrone
Quote:

I do not think that age should be initialised with a default value since it would be assigned a value very shortly thereafter. (This would have been more obvious if there were not those blocks of pedagogical comments in between.) test should not be initialised: it should be removed since it is never used.
it is always better to intialize varaiable while declaration:---

see this scenario

Code:

```int main() { int a; a += 10;```

as a will be garbage first it wont give right result then "(
• 10-09-2009
RockyMarrone
For the readability comments are there u can put if u dont use comment then this is more foolishness.
• 10-09-2009
laserlight
Quote:

Originally Posted by RockyMarrone
it is always better to intialize varaiable while declaration:

Well, if you really want to stick to your mantra of premature optimisation, then you should agree with me ;)

The snippet from the above program can be written in this way:
Code:

```cout << "Please input your age: "; int age; cin >> age;```
By declaring variables near first use, it is obvious that age will be assigned a value in the next line, thus initialisation is unnecessary. It could help as a pre-caution in case code (or comments, for that matter) gets inserted by a careless maintainer in between, but it could also confuse a reader who might wonder as to the reason for the initialisation when the value is about to be overwritten. (EDIT: That said, in this case since the return value of the call of cin >> age is not checked, a default value might make sense to serve as a flag in case the read fails.)

Quote:

Originally Posted by RockyMarrone
For the readability comments are there u can put if u dont use comment then this is more foolishness.

Then what happens when the comments go out of sync with the code? If you can write the code to be self-documenting, you should do so. Of course, in the event that you need to apply an optimisation (that is not obvious) where a bottle neck is found (and yes, you would have profiled the code to find that bottle neck), then writing a comment to explain the optimisation and the reason for it makes sense and is good practice.
• 10-09-2009
anon
Quote:

Any gain in the performance is very very huge it doesn't matter u r getting less or huge amount of performance gain......

If u will not think this way in embedded system programming then performance can't be improved
Complete non-sense. Here the time taken to output the message probably takes several orders of magnitude more time than the decision which message to output. You are most probably suggesting obfuscation of code in order to gain a microsecond of execution time (if you gain anything at all and if you don't in fact lose one or two microseconds), in an attempt to make the program some 0.000000001% faster. It would have to run in a loop perhaps thousands or even millions of years before anyone notices any difference in execution speed.

Also your mantra of initializing variables contradicts your mantra of premature optimizations. It is a complete waste of a nanosecond to initialize a variable to a value that you are never going to use.
• 10-09-2009
Elysia
Quote:

Originally Posted by anon
Also your mantra of initializing variables contradicts your mantra of premature optimizations. It is a complete waste of a nanosecond to initialize a variable to a value that you are never going to use.

Even better: initializing variables whose values will be overwritten shortly anyway. What a waste of execution time!