Thread: multiple if cond.

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    multiple if cond.

    Hi,

    Sorry about this noob question, but I'm obviously retarded or too tired to know why this isn't working:

    Code:
    for(int i = 0; i < 20; i++)
    	{
    		if(i%3 || i%5 || i%7)
    			cout << i << endl;
    		
    	}
    The output is the same as if there's weren't no if statement.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    yup -- it will print every loop value. Why? Pick any number between 0 and 20, then test it yourself.

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    Berkshire, UK
    Posts
    29
    You are actually asking for all values of i which are not divisible by 3, 5 or 7 which means that the first number you would not show is 21 (beyond your range). Perhaps you ment to use && instead?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    3*5*7 is greater than 20. That statement will only evaluate to false if 'i' is a multiple of 105.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Yes michaels that's what I wanted to do, but isn't || the OR operator? Meaning if i is divisible with 3 or 5 or 7, skip it?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by vikernes
    Yes michaels that's what I wanted to do, but isn't || the OR operator? Meaning if i is divisible with 3 or 5 or 7, skip it?
    Yes, but modulous evaluates to false (0) if it is devisible. So the expression is esencially if i is not divisible by 3, 5, or 7.

    So what you really want if either:
    if(!(i%3)||!(i%5)||!(i%7))
    or
    if(!(i%3 && i%5 && i%7))
    Last edited by King Mir; 05-29-2006 at 04:46 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM