Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: What 'C' output? And Why?

  1. #11
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,785
    the question is what is the final value of x, not what is the last value printed out by the loop. if you print 'x' again after the loop ends, you'll see the final value of 'x' is 10

    #include <iostream>
    using namespace std;

    void main()
    {
    for (int x=0; x<10;x++)
    {
    cout << x <<endl;
    }
    cout <<endl<<endl; //show it seperated from the loop output
    cout <<"the final value of x is: "<< x <<endl;
    return;

    }
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  2. #12
    Senior Member
    Join Date
    Jan 2002
    Posts
    187
    i believe that if the loop were written

    for(int i =0; i<10; ++i)

    it would be 10, because it would increment and then test.

    but the way harbir has it written, it will fail once i hits 10.
    U suk at teh intuhnet1!!1!1one

  3. #13
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Hey Tedob, I wanted to give you greenies, but the AP system thinks I have been a little to generous to you...you are absolutely correct, the final value of x would indeed be ten, I only considered inside the loop, but now maybe a question...you defined x in the loop...x outside the loop would be zero no? (damn I wish I was at work, would test that quick like...)

    /neb

    EDIT: undefined is what I meant, it should be undefined...sorry have always associated that with zero.
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  4. #14
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,785
    the loop made the value of x = 10, then failed. the value remains 10 unless you reset it. I ran the code before i posted it, and the value of x outside the loop is 10.
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  5. #15
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    The thing here is that, we are not concerned about the number of times the loop will run, All we are concerned is the final value of X, I have tested it also, The final value of X is 10.
    Explanation:
    Simple, if the value of X is nine, then the loop will run, it is onlt when X turns to 10 the condition turns False and the control comes out of the loop. Therefore the final value of X is 10.

    First even i got confused, But now i am sure, This answer is even right LOGICALLY.
    U get What U pay for.

  6. #16
    Jaded Network Admin nebulus200's Avatar
    Join Date
    Jun 2002
    Posts
    1,356
    Ok dang it, you made me vpn in and test it


    int main(int argc,char * * argv)
    {
    // int ;
    int i=0;
    for(int x=0; x < 10; x++)
    {
    printf("Loop i: %i %i", i++, x);
    }
    printf("Loop i: %i %i", i, x);
    }

    Using g++
    test.c: In function `int main(int, char**)':
    test.c:13: name lookup of `x' changed for new ISO `for' scoping
    test.c:9: using obsolete binding at `x'


    I guess it depends on your compiler, but x should be outside the scope of the for loop; however, depending on how the compiler references/dereferences the variable, the value of 10 was probably still out in the same memory address and x wasn't dereferenced and disrupted but other things writing to the same memory, then you would still get x=10 depending on if your compiler lets you do that...

    /neb
    There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.

    (Merovingian - Matrix Reloaded)

  7. #17
    Senior Member
    Join Date
    Oct 2001
    Posts
    114
    Do this in C and ull get the value of x 10 ( after the lop is over ) like all the right ppl above said do this in Java and nebulus is right .....
    Better Laugh At Your Own Problems..
    Coz...The World Laughs At Them

Posting Permissions

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