Nebojsa Tosic
Nebojsa Tosic

Reputation: 75

can someone explain behavior for the given loop?

for (k=0; tmp[j+k]=next[k]; k++);

when is this for-loop going to end and why?

Is it going to end when next[k] == 0 or when tmp[j+k] == 0? or is there something else that I am missing?

sorry for my bad eng. and sorry for noob question.

Upvotes: 2

Views: 80

Answers (3)

R Sahu
R Sahu

Reputation: 206717

The loop will continue until the value of the expression tmp[j+] = next[k] evaluates to 0. That will have happen when next[k] evaluates to 0.

If you are writing this code, or you are responsible for maintaining it, please make it more readable.

Use something along the lines of:

int valueToStopAt = 0;
for (k=0; (tmp[j+k]=next[k]) != valueToStopAt ; k++);

Upvotes: 0

It will end when the value of the expression tmp[j+k]=next[k] evaluates to zero. It so happens that the value of this expression is also the value of its right hand side: the value of next[k].

Upvotes: 0

Weather Vane
Weather Vane

Reputation: 34583

Notice that the loop has no body. The work is done in the end test. The end test is implicit, because although it assigns the value of next[k] to tmp[j+k], the expression also holds the value and is either 0 (false) or non-0 (true).

So the loop ends when 0 has been copied from one array to the other.

Upvotes: 3

Related Questions