EmbLogic's Blog

loop

for(y=1;y<10;y++)

x=y;

printf(“%d\n”,x);

printf(%d\n”,y);

it gives the output 9,10. why?

2 Responses to loop

  1. Varun Chaudhary says:

    for loop execute 9 times here.

    when y will be 9, the condition statement i.e. y<10 is true , so x=y i.e. x=9 will be executed, now compiler jumps to y++ increments y to 10, and checks the condition part where 10< 10 is false and for loop is not executed this time , for loop ends here.
    now x=9 here and y=10.
    :)

  2. niket says:

    with out curly bracket loop executed in single line so loop increment the value of y till 10 and x till 9 so it print 9 and 10 .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>