Thread: beginner's question :D

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

    beginner's question :D

    Hi all
    i am new to c programming (quite new to any type of programming, having only done a bit of python before), and i would just like to know if its possible (and how?) in c programming to:

    use an if statement with this sorta functionality

    if (condition) OR (condition)

    so that if either condition is satisfied the if statement is satisfied?

    thanksss!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Of course...tutorial is here.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    2
    ok cool thanks!

    that clarifies things for me
    so if i had a statement like this

    if (condition) OR (condition) OR (condition)

    and the 'false' of each of these conditions returns -1
    and the 'true' returns a positive integer?

    i would do something like

    Code:
    if ((condition1 + 1)|(condition2 + 1)|(condition3 + 1) != 0){
    etc etc

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    and the 'false' of each of these conditions returns -1
    and the 'true' returns a positive integer?
    So, you mean that if the variable's value is -1, the condition is false, but if the value is greater than zero, the condition is true? What if the value is 0?

    i would do something like
    Probably not. Assuming that a non-negative value means a true condition, you might do something like this:
    Code:
    if (condition1 >= 0 || condition2 >= 0 || condition3 >= 0) {
        /* etc etc */
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In C, false is 0, true is 1. So yes, you could add all the conditions and check if that sum is 0 (all three are false), or 3 (all three are true).

    Or as laserlight pointer out, you can use the logical operators 'or', 'and', 'not'.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kingliaho View Post
    if (condition) OR (condition) OR (condition)

    and the 'false' of each of these conditions returns -1
    In C the numerical value of false is zero
    and the 'true' returns a positive integer?
    The numerical value of true is non-zero so a condition may evaluate to -1 or any negative number but it would be true.
    It is upto the coder to do the error checking by testing the conditional evaluation and making their code more robust ie
    Code:
    if (condition > 0)
       /* start here if true */
    else
       /* if false begin here */
    Last edited by itCbitC; 10-17-2008 at 05:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 05:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM