Question: C Programming Can someone please explain me while the following (c) code prints out 6? for (i=1; x i=5, true, loop prints 5 ---> i=6,

C Programming

Can someone please explain me while the following (c) code prints out 6?

for (i=1; x<=5; i++);

printf ("%d",i);

i=1 initializes the the loop, it is being tested whether it is <= 5 or not. This statement is true, so it goes into the loop and prints 1.

After that, i++, means 1+1=2, i=2. This statement is being tested and it is true, we go into the loop and print 2.

Then i=3, true, loop prints 3 --> i=4, true, loop prints 4 --> i=5, true, loop prints 5 ---> i=6, false, no loop, don't print 6??!!

I have tried and changed the testing condition into i==0, and it still prints 1. Why would it do that? The Expression i=1 is not true?

Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!