Evaluating multiple expressions in an If statement.

This is a discussion on Evaluating multiple expressions in an If statement. within the C Programming forums, part of the General Programming Boards category; Hi there, My first post on this forum so please be gentle... how do i evaluate a variable to see ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1

    Evaluating multiple expressions in an If statement.

    Hi there,

    My first post on this forum so please be gentle...

    how do i evaluate a variable to see if it is equal to more than one value in an IF statement.

    For example if i want to do something based on if variable is equal to 5

    Code:
    if (variable == 5){
    //do something
    }
    else...
    but how do i write it if i want to do it based on if variable is either equal to 5 or 10?

    many thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you use "or" (also written "||").

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    if (x == y || x == z)
    Basically. Same for and.
    if (x == y && x == z)
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just to clarify a bit:
    Code:
    if (x == y && x == z)
    is the same as:
    Code:
    if (x == y && y == z)
    since x == y & x == z is only true if y == z (because x can only have one value, and if it's equal to y, then y and z must be equal for x == z to be true).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Ah yes, but I was demonstrating a common pitfall, such as:
    if (x == y || z)
    Which in real life would be read as if x is equal to y or z, but in programming it would be read by the compiler as
    if x equals y OR true (false)
    If z is anything else but 0, it will always be true and thus will not do what you expect.
    It is thus important to explicitly specify the variable to compare to. Unless you were comparing something as a boolean expression, of course, but many makes this mistake.
    The very same thing applies to and, as well.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Regular Expressions (regex.h) small problem
    By _Marcel_ in forum C Programming
    Replies: 0
    Last Post: 03-31-2009, 05:13 AM
  3. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  4. Multiple Definition Error
    By timmeh in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2009, 11:25 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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