Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
for(i=0;i++;i<100) loop is executed Zero times where else for(i=1;i++;i<100) is executed for infinite times. please Explain this .
XML
#include <iostream>

int main()
{
    int i;

    for(i=0;i++;i<100)
    printf("%d",i);

}


Executed Zero times

XML
#include <iostream>

int main()
{
    int i;

    for(i=1;i++;i<100)
    printf("%d",i);

}


Executed infinite times
Posted
Updated 25-Sep-15 19:56pm
v2
Comments
Kenneth Haugland 26-Sep-15 1:51am    
This does not seem right. Are you sure that you haven't switched places with i++ and i<100 ?
AWESOME AJ 26-Sep-15 2:08am    
Its a interview question . I am sure this was the question asked. I complied it too and it got the answer which is Zero and infinite respectively
Mohibur Rashid 26-Sep-15 8:51am    
?

Simple!
Since the for() last 2 parameters are swapped, j++ makes that the value of j is used to control loop exit.

In C, boolean values are coded this way:
- False is coded as 0 value
- True is coded as non 0 value

In first loop, j=0, so zero times
In second loop, j=1, so infinite times
 
Share this answer
 
Comments
AWESOME AJ 26-Sep-15 5:38am    
Thanks :)
  • First loop is exited immediately, because the termination condition is met immediately.


  • Second loop does't last forever, it terminates as soon i reaches 0, you have to wait a while (a big while), but it will eventually happen.


All in all your are using the for loop in a really strange, maybe unintended, way, so, why don't you check out the documentation?
Even a simple tutorial would do the job, see, for instance: "for loop in C"[^].
 
Share this answer
 
Because the expressions are in the wrong order. You say:
Set i=1.
if (i > 0) continue, and increase the value of i by 1.
At the end of each loop test i < 100, but take no action.

What you should be writing is:
C++
for (i = start value; while expression involving i; do something with i)

So for what you seem to be trying to do it would be
C++
for (i = 0; i < 100; i++)
{
// set i = 0
// if i is less than 100 continue
//    else terminate the loop
// at the end of each iteration of the loop add 1 to i, and test the repeat condition
}
 
Share this answer
 
FOR statements have the following syntax :
for( initialization ; termination ; iteration) where
initialization is what starts the loop
termination is how you determine when to end the loop
iteration is what happens on every iteration of the loop
so you should do : for(i=0;i<100;i++)

To answer your question why for(i=0;i++;i<100) does nothing depends on how the compiler generates your assembly language which depending on the compiler does something like
1 check termination
2 initialize
3 do loop
4 iterate
5 jump to 1

so in your case the termination is checked first which returns 1 i=1 on startup which is evaluated as true hence the code is terminated.
 
Share this answer
 
v2
Comments
Kenneth Haugland 26-Sep-15 2:26am    
I'm tempted to tell him to switch to vb instead:
For i as integer = 0 to 100 step 1
No-one can mess that up :)
AWESOME AJ 26-Sep-15 5:49am    
for(i=1;i++;i<100) return infinite loop. But in your explanation you are saying it is terminated How ?
Mehdi Gholam 26-Sep-15 5:54am    
Sorry, for i=0
Mehdi Gholam 26-Sep-15 5:57am    
In any case it depends on the compiler, you can use a disassembler to see what is generated.
Richard MacCutchan 26-Sep-15 8:49am    
termination is checked first which returns 1 i=1
Um, I rather think it returns zero. First i is set to zero, in the initialisation part. Next i is tested for non-zero, which is false, so the loop immediately stops. NB i++ returns the value of i before it is incremented.

Also, initialise is done only once and comes before check termination.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900