Thread: Check if a value is in a particular range.

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Check if a value is in a particular range.

    Hi,

    I want to test if the value in a variable is in a particular range. And i used the following program to test if what i'm thinkin is correct. However, when i ran that program, the statements execute even if the number is not in the range. I think it's the evaluation part which i've done wrong. Pls tell me the correct way thnx in advance!

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i = 5;
    
       if ( (1 <= i <= 5) )
          printf( "1 to 5\n" );
    
       if ( 6 <= i <= 7 )
          printf( "6 to 7\n" );
    
       if ( 8 <= i <= 10 )
          printf( "8 to 10\n" );
    
       getch();
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    just need to use && (AND)

    Code:
    #include <stdio.h>
    #include <conio.h> // and you need conio for getch() i think
    
    int main()
    {
       int i = 5;
    
       if ( i >= 1 && i <= 5 )
          printf( "1 to 5\n" );
    
       if ( i >= 6 && i <= 7 )
          printf( "6 to 7\n" );
    
       if ( i >= 8 && i <= 10 )
          printf( "8 to 10\n" );
    
       getch();
       return 0;
    }

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    thought of that, but is it the ONLY way ?

    if it is just don't reply. THnx a lot Brian.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    well you could do
    Code:
    if(i <= 1)
    {
    if(i >= 3)
    {
    printf("From 1-to-3");
    }
    }

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Or

    Code:
    if (a < 3)
    {
        ..
    }
    else if (a < 6)
    {
        ..
    }
    else if (...)
    ...

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Yeah, what shiro said.

  7. #7
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    you could always use a switch


    #include <stdio.h>
    #include <conio.h> // and you need conio for getch() i think

    int main()
    {
    int j, i = 5;

    printf("Enter number to test >");
    scanf("%d", j);

    switch(j)
    {
    case 1 :
    case 2 :
    case 3 :
    case 4 :
    case 5 : printf( "1 to 5\n" );
    break;
    case 6 :
    case 7 : printf( "6 to 7\n" );
    break;
    case 8 :
    case 9 :
    case10 : printf( "8 to 10\n" );
    break;
    default :;

    getch();/*i don't undersatnd what this is for */
    return 0;
    }
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 11:49 AM
  3. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM
  4. Please help to check.
    By nicoleha in forum C Programming
    Replies: 16
    Last Post: 12-07-2005, 03:29 PM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM