Thread: range of values in an if statement

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    range of values in an if statement

    In my if statement I want the values to be in a range to be within some range for the condition to be true.

    for 100 to 199 so i put

    if (num >= 100 && num <= 199) --- this is working

    200 to 500 and over 900
    else if (( num >= 200 || num <= 500) && (num > 900)) -- this is not working
    nor is (( num >= 200 && num <= 500) && (num > 900))

    " 501 to 900 except 700 to 750"
    I have tried

    else if (( num >=501 && num<=699) && (num>=751 && num <=900))

    but cannot seem to get it to work , I tried to change the || and && though non is working , Please corrected me , any comment on how can I go about this.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    In your condition (between 200 and 500 OR over 900), you just wrote ( (over 200 OR below 500) AND over 900) so you just wrote that part wrong, it should be :
    Code:
     if ( (num >= 100 && num <= 500) || num > 900)
    That is if I got it right. Try not to loose yourself in the AND and OR statements. A good way to be sure is to formally write it down with parentheses for the priorities then proceed to the replacements.
    Try to do it for your other problematic line, that'll do the trick, you'll see.

    Good day to you.
    Last edited by Meeshkah; 08-20-2010 at 05:28 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    For the second problem:
    Code:
    If (num >= 501 && num < 700 || num > 750 && num <= 900)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Preferably, use parenthesises to clarify precedence.
    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. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Checking Multiple Values in if() statement
    By Zero_X in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2006, 11:27 PM
  3. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 06:29 PM
  4. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM
  5. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM