Welcome to the VirtuQ Forums.
Results 1 to 3 of 3
  1. #1
    Member
    Join Date
    Oct 2011
    Location
    Allahabad, India
    Posts
    245

    For loop init statement

    Anup and Basant,

    Good Evening. Take a look on the code given below.

    #include <stdio.h>

    int main()
    {
    char i = 0;
    for(i<=5 && i>=-1; ++i; i>0)
    printf("%d\n", i);
    printf("\n");
    return 0;
    }

    How the initialization, condition, and counter is working, I am unable to figure out. I guessed, it works. But then also logic didn't strike.
    The result was
    1
    2
    .
    .
    127
    -128
    .
    .
    -1

    Thanks,

    Anon10020
    Anon 10020

  2. #2
    VirtuQ Moderator
    Join Date
    Jun 2011
    Location
    Bangalore, India
    Posts
    310
    Blog Entries
    4
    Anon10020,

    Let us consider a for loop:

    for(init_statement; condition; incr)

    The above for loop is equivalent to the following while loop:

    init_statement;
    while(condition){
    // loop statements incr;
    }

    Now why don't you write equivalent while loop and analyze your program.

    -- Basant

  3. #3
    VirtuQ™ Moderator
    Join Date
    Jul 2011
    Location
    Bangalore, India
    Posts
    1,044
    Blog Entries
    2
    Anon10020,

    It took me a while to figure out what all stupidities are going on in this question. Whosoever created that question in the first place relied on how the compiler will typically convert a for loop to assembly and arithmetic representation. In the line:

    char i = 0;

    has to represent a signed number in 8 bits, so, the number range is going to be -128 to 127. So, the numbers go on like this:

    Value Unsigned char Signed char
    0 (0x0) 0 0
    127 (0x7f) 127 127
    128 (0x80) 128 -128
    256 (0x100) 0 0

    Let's take the following for statement:

    for( i<=5 && i>=-1; ++i; i>0)

    The for loop is defined as follows:

    for(<Initialization statement, executed only once> ; <Condition which must evaluate to true or non-zero, executed every loop cycle> ; <Increment statement, executed every loop cycle> )

    In the above case:

    1. Initialization statement is: (i <= 5 && i >= -1), this is executed only once and the output of the expression is thrown away.
    2. Condition statement is: ++i, this is evaluated every loop iteration and the output must be non-zero
    3. Increment statement is: i > 0, is executed every cycle, but the output is thrown away.


    So, the loop will terminate if (2) above evaluates to '0' which in this case will happen when 'i' reaches the value of '0' after a wrap around after the value of '-1'.

    Statement (3) above is useless and you can as well have a ++j there, it will not cause any change to the output, try it out yourself.

    I hope this response is clear, however, this is a perfect example of "How not to do things"!

    Thanks,

    anup


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •