Thread: 'if' 'alway's true.

  1. #1
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172

    'if' 'alway's true.

    Hello all, i have a problem with an 'if' statement alway's being true. This is what i'm working on...

    Code:
    if (totalpoints > 150)  //when totalpoints exceed's 350, this need's to be false
    {
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    else if (totalpoints > 350) 
    {	
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    	gl_c = 4;
    }
    I've seen some interesting 'if' and 'else if' statement's before, what can i look for to increase my knowledge of these statement's, boolean algebra?
    Any help would be much appreaciated.
    Thank's.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As all numbers over 350 are also more than 150, the second boolean expression will never work.......

    Try this..

    Code:
    if (totalpoints > 150 &&  totalpoints < 350)  //when totalpoints exceed's 350, this need's to be false
    {
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    else if (totalpoints > 350)
    {
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    gl_c = 4;
    }

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    if (totalpoints > 150)  //when totalpoints exceed's 350, this need's to be false
    {
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    else if (totalpoints > 350) 
    {	
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    	gl_c = 4;
    }
    Remove the 'else' part

    OR

    Code:
    if (totalpoints > 150 && totalpoints < 350)  //when totalpoints exceed's 350, this need's to be false
    {
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    else if (totalpoints > 350) 
    {	
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    	gl_c = 4;
    }

  4. #4
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Thank's alot, i'm not quite sure how you learned that, i guess i do a search for boolean expression and see what i come up with.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    And what if totalpoints is 350 ???
    Code:
    if (totalpoints > 150 && totalpoints <= 350)  //when totalpoints exceed's 350, this need's to be false
    {
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    else if (totalpoints > 350) 
    {	
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    	gl_c = 4;
    }

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Or you could change the places of comparison.

    Code:
    /* totalpoints -> <350,...> */
    if (totalpoints > 350) 
    {
            printf("\n\n\nYou have earned %d points, you are at level 3\n\n\n", totalpoints);
    	gl_c = 4;
    }
    /* totalpoints -> <150,350] */
    else if (totalpoints > 150) 
    {	
            printf("\n\n\nYou have earned %d points, you are at level 2\n\n\n", totalpoints);
            gl_c = 2;
    }
    Last edited by Shiro; 06-18-2002 at 11:10 AM.

  7. #7
    Registered User loopy's Avatar
    Join Date
    Mar 2002
    Posts
    172
    Anyone searching for more info on boolean expression's look here
    I got this site from google.
    WorkStation(new, a month ago):

    Sony Vaio i686 Desktop
    2.60 GIGhz Intel Pentium 4(HT)
    512Mb DDR RAM
    800MHz Front Side Bus!
    120 GIG IDE HardDrive
    Matrox G400 Dual-Head
    Linux kernel 2.6.3
    Modified Slackware 9.1
    GCC/GDB

    Multi-mon
    Simultaneous Multiple Processes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM