Thread: Weird for loop.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Weird for loop.

    I just saw this weird for loop:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main(){
          int i, j;
    
          for(i=0, j=0; i <= 10; i++, j = i*i){
                  printf("i %d j %d\n", i, j);
          }
          printf("Overall: %d\n", ("abc", 1.2e6, 4*3+2));
          exit(EXIT_SUCCESS);
    }
    Please explain it to me, thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what don't you understand about it?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Why did they state 2 variables in that loop and then something like j = i * i and didn't state j in the 2dn paramater..
    the whole for loop actually.
    why did they printf the variable like this ("abc", 1.2e6, 4*3+2)? wtf?
    and what is exit(EXIT_SUCCESS);

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think that either somebody is trying to confuse you, or you're trying to confuse yourself. It's obviously obfuscated code.

    The comma operator evaulates the left hand side (lhs) and returns the right hand side (rhs).
    and what is exit(EXIT_SUCCESS);
    http://www.cplusplus.com/

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    what is the comma operator? never heard of it.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it's a comma. You know what a comma is don't you?

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Not really.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    This is ,

    ssharish

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Oh.

    But why does , relate to the for loop? :O

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    for(i=0, j=0; i <= 10; i++, j = i*i){
    To do two things at the same time. I mean u wanted to incr i and i*i at the same time. Thats why. You can do that using the comma operator.

    ssharish

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    I could do the same thing like this:
    Code:
    for (i=0; i <= 10; i++)
    j = i*i;
    So what's the point?

  12. #12
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by eXeCuTeR View Post
    I could do the same thing like this:
    Code:
    for (i=0; i <= 10; i++)
    j = i*i;
    So what's the point?
    Thats true, u reduce the line of instruction by embedding that into for loop

    ssharish

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Cool, thanks.

    BTW,
    check out my other thread here (1st page: 'Get source' thread) and help me out please

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,611
    You're allowed to set more than one variable and perform more than one action in a for loop. Recall that a for loop is written like so:
    Code:
    for ( initialize; test; action ) {
       [statements]
    }
    Your variables and the actions taken upon them can be the result of pretty much any function call or correct expression, but to do multiple things, the comma operator is used to ensure that they are evaluated, and evaluated in the correct order.

    > So what's the point?
    The point is, it's useful and not always so abused like in your example. A simple but, unfortunately, trivial example is reversing a C-string with pointers and a for loop.
    Code:
    char *head, *tail;
    char ch;
    for( head = cstr, tail = cstr + strlen( cstr ) - 1; head < tail; ++head, --tail ) {
       ch = *head;
       *head = *tail;
       *tail = ch;
    }
    This doesn't mean that you have to prefer writing this way, but it is a perfectly valid way to use a for loop and you deserve to be aware. Here's some recommended material.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Hey mate, why did you -1 when you strlen(cstr)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 11:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM