#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    56
    Rep Power
    0

    Which code u prefer & why


    Here are two codes written by me doing same thing. I want to know which code is better than the other if u give logical views about both then it will help me in improving my code writing style.

    1st code:

    PHP Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        
    int age;
        
    age=0;
        
    cout<< "Enter the age "<<endl;
        
    cin >> age;
        {
        if(
    age != 18)
        {
               {
                     if (
    age<18)
                     
    cout<<"U are a kid \n";
               }
               {
                     if (
    age >=25 && age<50)
                     
    cout<<"U are an adult \n";
               }
               {
                     if (
    age >=50)
                     
    cout<<"U are a budha baba now \n";
               }   
        }
        else
        
    cout<<"U are an adult \n";
        }
        
    system ("Pause");
        return 
    0;


    2nd code:


    PHP Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        
    int age1;
        
    age1 =0;
        
    cout<< "Enter the age "<<endl;
        
    cin >> age1;
        if (
    age1 >50)
        
    cout<< "You are an old man " <<age1 <<endl;
        if (
    age1 >= 18 && age1 <=50)
        
    cout<< "You are an Adult " <<age1 <<endl;
        if (
    age1 18)
        
    cout<< "You are a kid " <<age1 <<endl;
        
    system ("Pause");
        return 
    0;

    If u know any more appropriate way of writing the same code then share ur style so i can be having eye on those things as well in future while typing my codes in C++.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,325
    Rep Power
    2228
    Neither. Use if-else if. That way, in the second, first you test for >50 , then you else-if age1 >= 18. You don't need to test for age1 <= 50, because you already know that to be true (that's the else of the if age1 >50). Plus, you won't need to test for age1 < 18, because by getting there you already know that age1 is <18 so all you need is an else.

    Also, your way you need to perform each and every test, whereas by chaining if-else if's, you stop testing conditions as soon as you find one that is true.

    Also, please indent the code associated with an if-statement. Not indenting reduces your code's readability; eg, should be:
    Code:
    #include <iostream> 
    using namespace std; 
    int main() 
    { 
        int age1; 
    
        age1 =0; 
        cout<< "Enter the age "<<endl; 
        cin >> age1; 
        if (age1 >50) 
            cout<< "You are an old man " <<age1 <<endl; 
        else if (age1 >= 18) 
            cout<< "You are an Adult " <<age1 <<endl; 
        else
            cout<< "You are a kid " <<age1 <<endl; 
    
        // system ("Pause");  not needed; I always run console apps from the command line
        return 0; 
    }
    Last edited by dwise1_aol; March 8th, 2011 at 12:18 PM.
  4. #3
  5. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1111
    The logic of the first code is flawed. The condition shouldn't be if (age >=25 && age<50) , rather if (age >18 && age<50) .
    Apart from that, I would prefer the second approach as it uses 3 conditional branches, unlike the first one using 4 if conditions.

    I would do it like this though:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int age=0;
        cout<< "Enter the age "<<endl;
        cin >> age;
        if (age<18)
           cout<< "You are a kid "  <<endl;
        else if (age > 50)
           cout<< "You are an old man "  <<endl;
        else
           cout<< "You are an Adult " <<endl;
        return 0;
    }
    --------------------EDIT---------------------
    @dwise
    You came first :-P, We almost gave an identical solution though..
    Last edited by RAJ_55555; March 8th, 2011 at 12:28 PM.
  6. #4
  7. um, Hello?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    FN23fc
    Posts
    719
    Rep Power
    165
    Originally Posted by RAJ_55555
    The condition shouldn't be if (age >=25 && age<50) , rather if (age >18 && age<50) .
    This type of error shows the danger of performing essentially the same test more than once - it's easy for them to be different (and thus one or both is wrong) by either their being coded differently at the outset, or somehow modified differently later.

    The cascading if-else test that dwise and RAJ espouse is the right way to go.

    Comments on this post

    • RAJ_55555 agrees : Nice point..
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    56
    Rep Power
    0
    Thanks to all of u for clarifying the concept to me from now on-wards i have eye on this thing :)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2009
    Posts
    40
    Rep Power
    10
    Also avoid using if or similar statements without brackets, they are not needed for one line statements but it may lead to future errors when you'll modify the code. and you have to add brackets for adding more lines in a statement it's a waste of time.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Location
    Grand Rapids-ish, Michigan
    Posts
    142
    Rep Power
    61
    Okay, this may just be me thinking this, but wouldn't a case statement make the most sense here? I always thought it was bad practice to use if/else-if statements in place of a case statement...

    Someone correct me if I'm wrong about this though..
    Main Programming Language: C++
    IDE/Compiler: Code::Blocks v10.05 with mingw
    Operating System: Windows XP
    Language(s) trying to learn: Ruby
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    56
    Rep Power
    0
    Using break in any program can case serious logical errors or some time run times error I never ever prefeer to use such things
  16. #9
  17. Hats off to Mr. Joseph donahue
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2009
    Posts
    752
    Rep Power
    1111
    Originally Posted by David Tan
    Using break in any program can case serious logical errors or some time run times error I never ever prefeer to use such things
    That's bull****. In fact it's the perfect candidate for a switch case. It even has a performance gain over normal if-else conditional statements (provided there are enough conditions to deal with), not that it's significant in this case.
  18. #10
  19. um, Hello?
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    FN23fc
    Posts
    719
    Rep Power
    165
    Originally Posted by Larek Robinson
    wouldn't a case statement make the most sense here
    It might be too early in the semester for "switch/case" constructs. ;)
  20. #11
  21. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,966
    Rep Power
    9647
    Originally Posted by mike65535
    It might be too early in the semester for "switch/case" constructs. ;)
    He's in college? Looking to his spelling practices, I think he's still in grade school.

    Comments on this post

    • Schol-R-LEA disagrees : It seems more likely to me that English isn't his native tongue.
  22. #12
  23. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4302
    Originally Posted by Larek Robinson
    Okay, this may just be me thinking this, but wouldn't a case statement make the most sense here? I always thought it was bad practice to use if/else-if statements in place of a case statement...

    Someone correct me if I'm wrong about this though..
    switch ... case is a bad idea to use in this specific problem. For one thing, it doesn't support ranges, only exact matches. Which would you rather type out:
    Code:
    if (age < 18) {
        std::cout << You're a kid " << std::endl;
    }
    else if (age > 50) {
        // old fart
    }
    else {
        // you're an adult
    }
    vs.
    Code:
    switch (age) {
        case 1:
        case 2:
        case 3:
        case 4:
         ..... many more lines later ......
        case 17:
            std::cout << You're a kid " << std::endl;
            break;
    
        case 50:
        case 51:
        case 52:
            ... another gazillion lines later ...
        case 150:  // assuming that's the max. age anyone is going to get to
            std::cout << You're old " << std::endl;
            break;
    
        default: 
            std::cout << You're an adult " << std::endl;
            break;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  24. #13
  25. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Location
    Grand Rapids-ish, Michigan
    Posts
    142
    Rep Power
    61
    Ah, you make a good point there. Thanks Scorpion :)
    Main Programming Language: C++
    IDE/Compiler: Code::Blocks v10.05 with mingw
    Operating System: Windows XP
    Language(s) trying to learn: Ruby

IMN logo majestic logo threadwatch logo seochat tools logo