Thread: ok to return inside a for loop?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    34

    ok to return inside a for loop?

    Apologies for the noob question here, but it's been a very long time since did any C.

    can I just return a value from within a for loop to end loop execution or do I need to break and then return a value? Is this ok:
    Code:
    	int i;
    	for(i = 0; i < upper_limit; i++){
    		if (my_array[i] == val) {
    			return i;
    		}
    	}
    or must i do this?
    Code:
    	int i;
    	for(i = 0; i < upper_limit; i++){
    		if (my_array[i] == val) {
    			break;
    		}
    	}
    	if (i == upper_limit){
    		return -1;
    	} else {
    		return i;
    	}

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    First is right. You don't need a break here.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Of course you can. The for loop is just shorthand for:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        {
            int i = 20; // Initialize
            /* The loop */
            forloop1:
            printf("%d\n", i);
            /* The loop */
            if(!(i > 0)) goto forloop1end; // Check condition
            --i; // Iterate
            goto forloop1; // Loop
            forloop1end:
        }
    
        return EXIT_SUCCESS;
    }
    So inserting a return doesn't harm anything:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        {
            int i = 20; // Initialize
            /* The loop */
            forloop1:
            printf("%d\n", i);
            if(i == 5) return i;
            /* The loop */
            if(!(i > 0)) goto forloop1end; // Check condition
            --i; // Iterate
            goto forloop1; // Loop
            forloop1end:
        }
    
        return EXIT_SUCCESS;
    }
    Last edited by hauzer; 11-07-2011 at 04:09 PM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    34
    Thanks guys. I'll be using this:
    Code:
    int i;
    for(i = 0; i < upper_limit; i++){
        if (my_array[i] == val) {
            return i;
        }
    }
    return -1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  3. Replies: 9
    Last Post: 08-06-2009, 07:20 AM
  4. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM