beginner's question :D

This is a discussion on beginner's question :D within the C Programming forums, part of the General Programming Boards category; Hi all i am new to c programming (quite new to any type of programming, having only done a bit ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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
    21,711
    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 */
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,832
    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,047
    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, 06:00 AM
  2. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 04:53 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 04: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, 08:22 PM

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