Thread: Work with Boolean Operators

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    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.
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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";
             }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Quote Originally Posted by anon View Post
    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!

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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."
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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 "(

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    For the readability comments are there u can put if u dont use comment then this is more foolishness.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 10-09-2009 at 04:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    Last edited by anon; 10-09-2009 at 04:55 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by anon View Post
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with boolean operators
    By Melord in forum C Programming
    Replies: 2
    Last Post: 09-08-2009, 08:44 AM
  2. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  3. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM