Breaking out of a loop

This is a discussion on Breaking out of a loop within the C Programming forums, part of the General Programming Boards category; I'm just confused to think of a simple method to do this but Code: while (1) { length = recvfrom(s, ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    Breaking out of a loop

    I'm just confused to think of a simple method to do this but

    Code:
    while (1) {
    
              length = recvfrom(s, buffer, BUF_SIZE, 0, NULL, NULL);
    
               if (eh->h_proto == ETH_P_NULL && memcmp( (const void*)eh->h_dest, (const void*)src_mac, ETH_MAC_LEN) == 0 ) {
    			/* Comparing the Ethertype*/
    			if(memcmp((void*)(etherhead+14), FCoe,2) == 0 || memcmp((void*)(etherhead+14),FIP,2) == 0){
    				Ethfrm->ptr = (struct FCfrm_t *)(&Ethfrm->pad);  // Ethfrm pointer 
    			} else if(memcmp((void*)(etherhead+14), VLAN,2) == 0){
    				Ethfrm->ptr = (struct FCfrm_t *)(&Ethfrm->reserved);
    				printf("VLAN HEADER\n");
    				if((etherhead[16]& 0x10) == 0x10){
    					//printf("readint %02X \n",etherhead[16]);
    					printEthfrm(Ethfrm);
    					//break;
    				}
    			} else {
    				printEthfrm(Ethfrm);
    				//break;
    			}
    		
    			/* Setting the FCframe_t StructureStart Pointer to the EthernetFrame*/
    			Ethfrm->ptr->StructureStart = Ethfrm;
                           ....
    			.....
    			....
                 }
    }
    Everytime i break i break out of the while loop but I want to break out of the if statement with the true arguement and I'm kinda not sure how to do it simply without just overdoing it.
    Last edited by kiros88; 09-01-2009 at 12:42 PM. Reason: Sorry rather just put the real code

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't break out of an if statement. If you don't want the rest of the material in if(true) to be done when blah is true, then do "if (!blah) //all the other stuff".

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    Sorry bout that so like this is my setup and theres alot more code underneath. So I don't know where to put the if(!arguement)anymore but I need it so i do a printEthfrm(). Then just exit out of the if (eh->h_proto == ETH_P_NULL && memcmp( (const void*)eh->h_dest, (const void*)src_mac, ETH_MAC_LEN) == 0 ) statement and start back to the revcfrom() portion of the code.
    Last edited by kiros88; 09-01-2009 at 12:46 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Quote Originally Posted by kiros88 View Post
    Sorry bout that so like this is my setup and theres alot more stuff so iono where to do the if(!blah)anymore but I need it so i do a printEthfrm() then just exit out of the if statement and go back to the revcfrom()
    You need to end your sentences with periods. I have no idea what you are trying to say.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    iono where to do the if(!blah)
    Assuming "iono" means "I don't know", The if(!blah) should simply be the stuff you don't want executed under 'blah' conditions. Of course, iono isn't really a word so I haven't got a clue what you really mean.

    However, if you just want to go back to the call to recvfrom(), you should look into the continue statement. It just jumps you to the next iteration of the loop.

    edit: based on your edits - I'd say you just need to add a "continue;" statement after your call to printEthfrm();

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    None of your break statements, if they did what you want, would skip any code! If I were you, I would stop to think about what you actually want to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 06:50 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  5. Breaking out of a loop
    By escarrina in forum C Programming
    Replies: 9
    Last Post: 04-05-2004, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21