# Thread: order of increment regarding condition..

1. ## order of increment regarding condition..

in a for loop
first is gets the initializatoin value checks if it ok by the condition .

does is increment and then checks the condition

or the other way around
??

2. The increment happens at the end of the loop. So it goes:

1. Initialize
2. Check
3. If check doesn't pass, terminate loop
4. Do loop body
5. Increment
6. Go to step 2

3. 3. If check doesn't pass, terminate loop
4. Do loop body

you say that it does 4 after 3??

4. Code:
`for( 1 ; 2 ; 4 ) { 3 }`
Quzah.

5. if it terminates the loop in step 3
then why in step 4 it goes inside
??

6. It doesn't terminate in step three. That was illustrating how the four steps go. Sheesh, we've only explained this three times now.

First it checks ... wait for it ... 1. There it initializes variables and does whatever's there.
Then it goes to ... you guessed it, 2. It checks the condition of the loop. If that's false, it immediately exits, without doing 3 or 4.
Then it jumps to the body of the loop, marked oddly enough as 3, in the above illustration.
Finally, after it executes the body of the loop, it jumps to 4.

After it's gone through step four, it goes back to the condition, which is to say, step 2. Then it sort of ... loops. For however long it's supposed to. Until the condition has been reached, or until you break out of it prematurely. Funny how that works.

Quzah.

7. i got this code and in the first for loop inside the big (marked red)one when the pointer is on the last node before null it goes next and still
goes into the loop instead of going out(thus contradiction the condition of the loop)
??

when it goes to null it shul getout of the loop
why it goes in??
Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct node {
int val;
struct node * next;

}node;

int main()
{
node* start=(node*)malloc(sizeof(node));
start->val=2;
start->next=(node*)malloc(sizeof(node));
start->next->val=1;
start->next->next=(node*)malloc(sizeof(node));
start->next->next->val=2;
start->next->next->next=(node*)malloc(sizeof(node));
start->next->next->next->val=2;
start->next->next->next->next=(node*)malloc(sizeof(node));
start->next->next->next->next->val=4;
start->next->next->next->next->next=(node*)malloc(sizeof(node));
start->next->next->next->next->next->val=3;
start->next->next->next->next->next->next=(node*)malloc(sizeof(node));
start->next->next->next->next->next->next->val=4;
convert(&start);

return 0;
}
node *prev1 = NULL, *temp1, *temp2, *prev2, *temp3;
int flag,i;
flag=0;
for(temp2=temp1->next, prev2=temp2; temp2; temp2=temp2->next){
if (temp1->val == temp2->val){
prev2->next = temp2->next;
free(temp2);
temp2 = prev2;
flag=1;
}
else prev2 = temp2;
}
if (flag){
if (!prev1){
temp2 = temp1;
temp1 = temp1->next;
free(temp2);
}
else{
prev1->next = temp1->next;
free(temp1);
temp1 = prev1->next;
}
}
else{
temp3 = temp1->next;
for (i=0; i<temp1->val -1; i++){
temp2 = (node*) malloc(sizeof(node));
temp2->val = temp1->val;
temp1->next = temp2;
temp1 = temp2;
}
temp1->next = temp3;
prev1=temp1;
temp1=temp1->next;
}
}
}```
??

8. I don't see why it wouldn't get out of the loop.

--
Mats

9. because temp2 is null and that contradicts the condition
; temp2;

it shouldnt go in when its null

What rubbish compiler are you using? If you are using a decent compiler it will not enter that loop if temp2 is NULL.

How can you be sure that it actually enters the loop?

--
Mats

11. because i debugged it and i got runtime
null accssess memory error

12. Considering the rather complex code, I wouldn't surprise me if there is some bug in your code. But I doubt that is because the code enters a loop it shouldn't enter.

And you probably should set start->next->next->next->next->next->next->next [I think that is the right number of next] to NULL at the end of the initialization of your linked list (have you ever thought of using a temporary variable for the "tail" of the list, so that the code gets a bit more readable?)

--
Mats

13. so at first it initializes and goes inside the loop
then it increases and then it checks the condition
if its ok by the condition then it goes inside the loop
if not ,it goes out of the loop

etc..

correct?

14. Let's try again with an enumerated list. I'll use LETTERS as to not confuse it with the two different numerical systems produced earlier:
A. Initializes.
B. checks the condition and exits if false
C. do the loop.
D. Increment
E. Go to step B.

When I first ran your code, I did get the problem you describe at an address outside your code - probably in free.

I've now fixed the "next = NULL" at the last entry in the linked list, I don't get that problem, but instead it seems to form an infinite loop inside the first loop. I'm trying to understand what your code is supposed to do, but it's not entirely clear to me.

--
Mats

15. yes i put null in the end and it worked fine
thanks for clearing this for loop stuff