Computer Science, asked by Pikachu82, 1 year ago

can we write a for loop without initialization? if yes give example

Answers

Answered by garywalter1221
14

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


Pikachu82: it's ok but tq showing helpfulness
garywalter1221: you're welcome
Answered by Sidyandex
52

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.

Similar questions