Thread: For loop and semantic manipulations

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    For loop and semantic manipulations

    Hi !
    can someone please explain to me exactly how the for loop work?! I mean when exactly the "i++" modified? when entering the loop or once returning from the loop it gets modified? also is it first gets modified and then check the condition of for loop or first checking the condition and then adding the "i"?

    if after loop itself I added the operand "i" will be it added again once returning to the loop itself? I mean like this:
    Code:
     for (int i=0;i<10;i++)
    {
    i+=5;
    }
    then will I be added again by 1 once returning to for itself?

    what's the difference between: "++i", "i++";

    also what's the difference between those:
    for(int i=0; i<10;i++);
    for(int i=0;i<10,++i);
    a[i]=a[i++];
    a[++i]=a[i];
    a[i++]=a[i];

    thanks in advance and much appreciate your help!!
    Last edited by RyanC; 12-31-2018 at 09:18 AM.

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    92
    Have you read any book on C ? If you read several top C books, many of such questions are answered. Generally I would say try to do one job in every line as far as you can. Also take a look at this link about C operator precedence.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    Quote Originally Posted by RyanC View Post
    also what's the difference between those:
    for(int i=0; i<10;i++);
    for(int i=0;i<10,++i);
    a[i]=a[i++];
    a[++i]=a[i];
    a[i++]=a[i];
    There's no difference between the first two.

    The last three have undefined behavior--don't do them!

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    A for loop is composed of three sections; each separated by a ';'.
    First of all, they are all optional. You can omit one or all sections.
    Code:
    for(;;); // This is valid. A loop that runs forever.
    In the first section you can define or initialize a variable. Usually the variable
    that controls the loop, but it can be any variable.
    Code:
    //declaration and definition
    for (unsigned int i = 7;;)
    {
        //loop body...
    }
    
    //initialization
    unsigned int i;
    for (i = 42;;)
    {
        //loop body...
    }
    The second section evaluates an expression at the beginning of each iteration.
    If true, the loop body will execute, if false, control jumps to the next instruction
    after the loop.
    Code:
    for (;2 + 2 == 4;)
        printf("Seems like two plus two is four!");
    The last section executes an instruction. Simple as that.
    Code:
        for(;;printf("Printing hello world forever!\n"));
    Although commonly used to control the expression that is evaluated to determine
    wether the loop executes or not, it doesn't have to be that, as you can clearly
    see on the example above.

    Now, I'm showing you this to help you understand the mechanics of the "for" construct,
    and that there is nothing mystic about it. This doesn't mean however that you should
    use the "for loop" however you want. You could, but you shouldn't.
    It will be more clear for anyone reading your code if you stick to the common conventions
    of defining the exit condition, evaluating the exit condition, and modifying the exit condition:
    Code:
    for (unsigned int a = 10; a > 0; --a)
    {
        printf("Countdown to new year: %u!\n", a);
    }
    printf("Happy new year!\n");
    [/code]

    ++ and -- are the increment and decrement operators. As their name suggest they
    either increment or decrement an object or expression.
    Code:
        a = a + 1;
        //is the same as
        a++;
        //or
        a += 1;
    These operators can be used either before or after the expression they modify, i.e
    in prefix or postfix form.
    When used before, their modification is given priority and occurs before most other operations.
    In case of simple expressions where there are no more operations, like in the example above,
    the expressions are equivalent.
    Code:
    ++a;
    //is the same as
    a++;
    In cases like:
    Code:
    unsigned int a = 7;
    unsigned int b = 0;
    
    b = a++;
    There is a difference. Here b will equal 7 because the ++ operator is used in its postfix form;
    the assignment operation is performed first, b = 7, then the increment operation takes place, a++ or a += 1,
    which results in a being equal to 8. We end up with a = 8, b = 7.
    If we use the prefix operator instead we get:
    Code:
    unsigned int a = 7;
    unsigned int b = 0;
    
    b = ++a;
    In this case the increment operation takes precedence and 'a' is incremented before the assignment;
    a += 1 equals 8, then b = 8, resulting in both a and b being equal to 8.

    Increment and decrement operators - Wikipedia

    You do need to be careful when using these in certain expressions like:
    Code:
    a[++i]=a[i];
    a[i++]=a[i];
    This is undefined, and you should get a warning, due to your modification of the element used to access the array.
    This has to do with how the expression is evaluated at a lower level, I don't quite understand the details myself.
    I do know some compilers will correctly deduce your intent and work as you'd except while others will crap on you.
    Last edited by Dren; 12-31-2018 at 09:47 PM. Reason: Added increment/decrement operators bit.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    The goof-ball has also infested another forum now.
    For Loop - C And C++ | Dream.In.Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2018
    Posts
    38
    Quote Originally Posted by Dren View Post
    ... book elided ...
    Stop feeding the troll.
    Just let it die.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,532
    No more food.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault with ppm manipulations.
    By Cody Hutto in forum C Programming
    Replies: 3
    Last Post: 04-17-2015, 02:24 PM
  2. PPM Image Manipulations
    By hbranum in forum C Programming
    Replies: 10
    Last Post: 12-07-2012, 07:53 PM
  3. Bitwise Manipulations in c
    By MrWannabe in forum C Programming
    Replies: 1
    Last Post: 09-22-2009, 11:02 PM
  4. Replies: 17
    Last Post: 02-19-2009, 11:49 PM
  5. semantic analyer..need help!!!
    By Sakuragi in forum C Programming
    Replies: 1
    Last Post: 02-07-2002, 11:28 AM

Tags for this Thread