I need help with C

for (index=10; (index < 20); index++


what is the value of index?

is it 20 or 21?


does the incrementation count for this? or does it leave it off?


same with while and do-while? I think I will stick with being the loops having the incrementation field valid, is it?


Comments

  • : for (index=10; (index < 20); index++


    : what is the value of index?

    : is it 20 or 21?


    During the loop, it would be 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, and 20 in successive order. After the loop, assuming you didn't break out prematurely, index would be 21 (the first value index can become that fails the condition of the loop).




    : does the incrementation count for this? or does it leave it off?


    I have no idea what you're saying here. The incrementation is ultimately responsible for it inasmuch as index takes on successive values in the loop until the condition fails.




    : same with while and do-while? I think I will stick with being the loops having the incrementation field valid, is it?


    Your for loop is fine, I guess, except that it doesn't have a close-parenthesis and doesn't have any associated code that will repeatedly execute.


    while and do-while don't have the same format as a for loop. while and do-while simply continue to loop as long as the condition is true. There is no initialization or alteration (incrementation) field.


    Regardless, if you put garbage in, you'll get garbage out. If your for loop or while loop or in fact any bit of C code doesn't make sense it either won't compile, won't link, or won't run the way you expect.


  • A Simple program show you that i is 20 after the loop:

    #include


    int main () {

    int i;

    for (i=10;i<20;i++) {<br>
    cout << "Loop " << i << " ";<br>
    }

    cout << "Out " << i;<br>
    }


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories