Check if a value is in a particular range.

This is a discussion on Check if a value is in a particular range. within the C Programming forums, part of the General Programming Boards category; Hi, I want to test if the value in a variable is in a particular range. And i used the ...

  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,220
    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,220
    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,386
    Or

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

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    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-06-2010, 12:42 AM
  2. My own itoa()
    By maxorator in forum C++ Programming
    Replies: 18
    Last Post: 10-15-2006, 12:49 PM
  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-25-2002, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21