can we write a for loop without initialization? if yes give example
Answers
I think if we will not initialize then loop will go infinite i.e infinite loop
I'm not so sure about this
Because I've tested this in C language i.e not using initialization in for loop
Yes.
A 'for' loop can be written without initialization.
A 'for' statement usually goes like: for (initialization; test-condition; update).
We can leave out any or all three of them at a time.
Therefore, for (;;) is a kind of infinite loop1 that is equivalent to 'while' (true) as there is no needed test condition.
As a matter of fact, for (int i=0; ;i++) is a kind of infinite loop1.
For ( ; *s != '\0'; s++) is a type of loop that has no initialization.
's' is the beginning point of a string and can be incremented until it meets the null character '\0' which shows the end-of-string.
It primarily means a loop through all the characters of 's'.
However, the loop can also be broken if there is a break statement in the body of the loop or there is a call to exit(), etc.