2

I am new to C and have to write some code that emulates a given function. However, I am having a difficult time understanding what the second for loop in this code is doing, explicitly. The syntax doesn't seem to follow standard for loop syntax of:

for ( init; condition; increment ) {
statement(s);
}

Here is the code I am examining and it's the second for loop that I am not following and I don't see any online version of this

for (i = 0; i <= (n1-n2); i++){   
    count=0;
    for(j = i,k = 0; k < n2; j++,k++){
        if (*(s1+j)!=*(s2+k)){
            break;
        }
        else
            count++;
        if(count==n2)    
            total++;                         
    }
}

I'm assuming it's two for loops in one, since there are two increments. And I think the inequality is similar to a 'while loop', but I'm not certain. The confusing piece is that there doesn't seem to be a condition for the j loop. I'm not sure if it's a syntax shortcut or if there is a special loop I can not find a resource for online.

1
  • k < n2 is the condition for the inner loop - there is no requirement that it has to use the variable j Commented Apr 22, 2017 at 16:18

3 Answers 3

6

I'm assuming it's two for loops in one, since there are two increments.

Well, not really. The second for loop also follows the prescribed syntax, it's just a little fancy about handling the loop related variables.

For a loop statement like

 for(j = i,k = 0; k < n2; j++,k++){

can be interpreted as

 for( (j = i,k = 0)  ; (k < n2) ; (j++,k++) ){
       ^^^^^^^^^^^      ^^^^^^     ^^^^^^^  
       init            condition    post loop body operation.

Now, the initialization and post-body expression blocks makes use of comma operator.

Related, quoting C11, chapter §6.5.17

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

To elaborate, the same syntax can be broken down to

 j = i;                               //initialization, only once
 for( k = 0  ; k < n2 ; k++ ){        // good ol' syntax
  //......loop body
  j++;                                //last expression in the body.
 }                                    //end of loop
Sign up to request clarification or add additional context in comments.

8 Comments

Ok that is fantastic. So the j is 'along for the ride' sort of speak. And the k loop acts as a 'while' loop, it counts from 0 to 1 less than n2. is that an appropriate analogy?
@Joseph yes, almost, adding to that, j is also increased alongside k after every loop body execution.
So I ran the code and it appears the k loop resets on the k < n2 condition. The j loop continues counting until the 'break' condition is met in the if statement. Is that what should happen?
@Joseph what do you mean by the k loop and j loop?
I need to get the right terminology - but i'm fairly certain I understand the syntax now. thank you!
|
3

You can do multiple initialization and increment in a single for loop exactly as in your example (separated with a comma).

for(first = 0, second = 0 ; your_condition ; ++first, ++second)
    ...

This is useful when you need multiple variables to be updated each time in your for loop.

Hope this helps!

2 Comments

So the your_condition is the same ending condition for both loops?
@Joseph this is not a "two for loop in one", this is a single for loop with multiple variables. Thus, the for loop only needs one condition. ;)
0
for(j = i,k = 0; k < n2; j++,k++){
    initialisation  condition   increment or decrement
  1. here they are considering two variables to keep track of.
  2. Its not two for loops in a single for loop
  3. the reason its done like that is to initialize and update variables each iteration.
  4. Though you may have issues if you are using multiple type variables inside for loop for initialization

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.