Less than or equal - precedence in for loops

This is a discussion on Less than or equal - precedence in for loops within the C Programming forums, part of the General Programming Boards category; Some thing that I've been wondering about for a while, is the precedence of < (less than) and <= (less ...

  1. #1
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Question Less than or equal - precedence in for loops

    Some thing that I've been wondering about for a while, is the precedence of < (less than) and <= (less than or equal). As far as I know, these operators have the same precedence.

    Now, take this for an example:
    Code:
    	int	i;
    
    	for (i = 0; i < 4; i++)
    	{
    		printf ("%d\n", i);
    	}
    Would print 0, 1, 2, 3. However, if I change it so:
    Code:
    	int	i;
    
    	for (i = 0; i <= 4; i++)
    	{
    		printf ("%d\n", i);
    	}
    It prints 0, 1, 2, 3, 4. So what's the deal? In the second code snippet, it loops while i is less than or equal to four. In the first example, it loops while is less than four. Can someone explain to me why this behaviour - i.e. the second example should (in theory) yield the same results as the first example, right?
    Chaos, panic, pandemonium... My work here is done!

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    No.

    Precedence is the same -- precedence only means the order of operations. In arithmetic, + and - have the same precedence, but they aren't the same operation at all.

    As you say, the 2nd code loops while i is less than or equal to 4. That's the condition in the loop -- whenever the middle part is true, the loop executes again.

    So if the condition is i < 4, then it will execute as long as i is less than 4. When the condition is i <= 4, it loops as long as i is less than or equal to 4.

    Precendence has absolutely nothing to do with this; precedence only affects expressions with many parts. E.g.

    i = 1 + 2*3;

    is guaranteed to yield 7, not 9, because the "*" operator is higher precedence than "+", so the multiplication, being higher precedence, happens first. The assignment operator, "=", is very low precedence and happens last. The above assignment evaluates just like:

    (i = (1 + (2 * 3)));

    and NOT like:

    (i = ((1 + 2) * 3));
    or
    (((i = 1) + 2) *3);
    or anything else.

    Precedence only impacts the order of operations in a single expression. It doesn't mean that < and <= give the same answer (and intuitively, they should NOT. 4 < 4 is false, 4 <= 4 is true).
    Last edited by Cat; 06-23-2003 at 05:59 PM.

  3. #3
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11
    "Now that I come to think of it..." - Thanks, that answered my question!

    Edit: Oops!
    Last edited by Idle; 06-23-2003 at 06:12 PM.
    Chaos, panic, pandemonium... My work here is done!

  4. #4
    twigg
    Guest
    for (i = 0; i <= 4; i++)

    For i is equal to zero, and is less then or equal to 4, something is done, at the end one is added to i.

    If you put
    for (i = 0; i == 4; i++)

    i must = 0, and 4 at the sametime to do anything. Which is impossible. Thats why nothing is printed.

    I'm still learning C, so I may be wrong. So don't quote me.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by twigg
    If you put
    for (i = 0; i == 4; i++)

    i must = 0, and 4 at the sametime to do anything. Which is impossible. Thats why nothing is printed.

    I'm still learning C, so I may be wrong. So don't quote me.
    Yes and no. Yes you're right, but no, it's not worded very well.

    First you give "i" the value of zero.
    Next, you say if this test is true, do whatever is in the loop. The test being "i == 4" fails because you have set it to 0.
    The loop ends, and "i" is never incremented. The body of the loop is never executed.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C4554 Warning, check operator precedence for possible error
    By Bassquake in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 05:13 AM
  2. Equal compare
    By George2 in forum C Programming
    Replies: 1
    Last Post: 11-10-2007, 04:26 AM
  3. set string array equal variable
    By WaterNut in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2004, 05:02 PM
  4. precedence / associativity
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2003, 10:03 AM
  5. how can 0.00665 not equal 0.00665??!!
    By Susan in forum C++ Programming
    Replies: 10
    Last Post: 02-10-2002, 01:33 AM

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