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
??
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
??
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
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
3. If check doesn't pass, terminate loop
4. Do loop body
you say that it does 4 after 3??
Quzah.Code:for( 1 ; 2 ; 4 ) { 3 }
Hope is the first step on the road to disappointment.
if it terminates the loop in step 3
then why in step 4 it goes inside
??
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.
Hope is the first step on the road to disappointment.
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; void convert(node** head); 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; } void convert(node** head){ node *prev1 = NULL, *temp1, *temp2, *prev2, *temp3; int flag,i; for (temp1=*head; temp1; ){ 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); *head = temp1; } 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; } } }
I don't see why it wouldn't get out of the loop.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
because temp2 is null and that contradicts the condition
; temp2;
it shouldnt go in when its null
Answer Alternative A:
What rubbish compiler are you using? If you are using a decent compiler it will not enter that loop if temp2 is NULL.
Answer alternative B:
How can you be sure that it actually enters the loop?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
because i debugged it and i got runtime
null accssess memory error
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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?
Last edited by transgalactic2; 04-09-2009 at 03:37 AM.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
yes i put null in the end and it worked fine
thanks for clearing this for loop stuff