Thread: order of increment regarding condition..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    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. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    3. If check doesn't pass, terminate loop
    4. Do loop body

    you say that it does 4 after 3??

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( 1 ; 2 ; 4 ) { 3 }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if it terminates the loop in step 3
    then why in step 4 it goes inside
    ??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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;
    		}	
    	}			
    }
    ??

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    because temp2 is null and that contradicts the condition
    ; temp2;


    it shouldnt go in when its null

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    because i debugged it and i got runtime
    null accssess memory error

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes i put null in the end and it worked fine
    thanks for clearing this for loop stuff

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  3. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  4. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM