multiple conditions - if statement

This is a discussion on multiple conditions - if statement within the C Programming forums, part of the General Programming Boards category; Hi, In an if statement i want to check multiple conditions (for the same result), but am thinking that there ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bristol, UK
    Posts
    4

    multiple conditions - if statement

    Hi,

    In an if statement i want to check multiple conditions (for the same result), but am thinking that there is a quicker way....at the moment ive got it working as follows:

    Code:
    if('x' == 0 && 'y' == 0 && 'z' == 0)//and 16 more variables ==0!
    {
     //do something
    }
    as you can see the if statement will go on forever! Any suggestions?

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Use an array?
    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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bristol, UK
    Posts
    4
    would this syntax work?


    Code:
    int num[4];
    
    if(num[0, 1, 2, 3] == 0)
    {
    //do something
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    No, it would not do what you intend as it would be effectively:
    Code:
    int num[4];
    
    if(num[3] == 0)
    {
    //do something
    }
    due to the comma operator.
    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
    Mar 2009
    Location
    Bristol, UK
    Posts
    4
    Sorry if stupid question, but what do you mean due to the comma operator? That syntax works when assigning values to the elements....im confused :/

    what would the correct syntax be?

    thank you for your help!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by dibble
    Sorry if stupid question, but what do you mean due to the comma operator? That syntax works when assigning values to the elements....im confused :/
    That is, the expression 0, 1, 2, 3 evaluates to 3. If you want to see this effect in an example, figure out the output of this program, then compile and run this program to see if you guessed correctly:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int n = (1, 2);
        printf("%d\n", n);
        return 0;
    }
    Quote Originally Posted by dibble
    what would the correct syntax be?
    What you did originally, or by using an array with a loop.
    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

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    Bristol, UK
    Posts
    4
    I can see how that works. Thank you for your help, much appreciated.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you can't (or don't want to ) use an array, you can assign the variables to a pointer array:
    Code:
    #include <stdio.h>
    
    int main() {
    	int a=1, b=4, c=9, *ray[3], i;
    	ray[0]=&a; ray[1]=&b; ray[2]=&c;
    	for (i=0;i<3;i++) if (*ray[i]==4) printf("Found.\n");
    	return 0;
    }
    ps. I think you should use brackets around each truth test of this sort:
    Code:
    if((x == 0) && (y == 0) && (z == 0))
    You DEFINATELY should not be enclosing the variable names in single quotes('x', 'y','z'); the only reason this compiles and runs is because you are in effect testing the ASCII values (qv.) of a character. That means your condition tests had absolutely nothing to due with any int variables named x, y, or z.

    A quick demonstration of this principle:
    Code:
    #include <stdio.h>
    
    int main() {
    	if ('a' == 97) printf("True");
    	return 0;
    }
    Notice there is no variable named a, but the ASCII value of 'a' is 97. Even if you add "int a=5" at the beginning, the value of 'a' is 97, so the condition will still evaluate as true (ie, the character 'a' and a variable, a, are not the same thing).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by MK27
    If you can't (or don't want to ) use an array, you can assign the variables to a pointer array:
    hmm... but if there is only one if statement, that would be excessive work since you might as well just write out the various comparisons directly.

    Quote Originally Posted by MK27
    ps. I think you should use brackets around each truth test of this sort:
    That is optional here. I feel that the extra parentheses are superfluous in such cases, but other people think that it is good style.

    Quote Originally Posted by MK27
    You DEFINATELY should not be enclosing the variable names in single quotes('x', 'y','z');
    Good catch
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Definition Error
    By timmeh in forum C++ Programming
    Replies: 9
    Last Post: 02-15-2009, 11:25 AM
  2. Evaluating multiple expressions in an If statement.
    By bitslap in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 04:38 AM
  3. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  4. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

Tags for this Thread


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