chevron_left chevron_right
Login Register invert_colors photo_library
Thread Rating:
  • 0 Vote(s) - 0 Average


filter_list Tutorial Break and Continue
Author
Message
Break and Continue #1
This one was suggested by @Mr.Kurd based on my short tutorial about for loops.

So, what do the keywords break and continue do, and how do they work?

Let's take a look at an infinite wait loop:
Code:
bool flag;
flag = false; //this will be set to true by another thread
while (true)
{
   if (flag)
       break; // exit the loop
}
Ok, so this is a pretty basic example, the loop runs until flag gets set. Do not confuse this with a while(flag); loop, it's not the same.
Basically, what happens is that whenever a break is encountered, it will jump up exactly one level in a loop.

Continue works in the same sort of way. You'll understand it a little bit better once you look at the code for this one.
Let's say, we want a while loop that will initialize itself (for some reason):

Code:
int *val;
bool flag;
val = NULL;
flag = false;
while (true) // indefinite thread loop
{
   if (val == NULL) // it's not allocated
       continue; // jump back up so we don't use val until the thread allocates it
   *val = 5; // set it to something, or do some calculation on it
   if (flag)
       break;
}
Now, this could act like a memory manager, garbage collector, whatever. It's only job is to constantly monitor val and make sure that it exists. If it got deleted, it recreates it again. Once flag is set (like in the event that the child thread is done), the loop exits.

You can also use break as an error check within loops. For example, here's a while loop that will find the end of your linked list:
Code:
struct llist *result;
result = <input list>;
while ((result = result->next) != NULL)
   if (result->next == NULL)
       break; // result is now the end of the list

I know this one was even shorter, these are basic concepts. Maybe I'll keep writing these small ones out and eventually compile them into some form of SL book. Let me know what your questions are.
I will keep that list up to date at all times. Check often for new materials!

[+] 1 user Likes phyrrus9's post
Reply

RE: Break and Continue #2
While this is far being my skill sets, I appreciate the fact that you go into such detail with tutorials for members here.

We thank you bro!
[Image: THqqjY5.gif]

Reply

RE: Break and Continue #3
(10-10-2017, 07:59 AM)Slacker Wrote: While this is far being my skill sets, I appreciate the fact that you go into such detail with tutorials for members here.

We thank you bro!

I don't think it's over your head. Some of the examples may be, but consider it like this:

When you shower, the shampoo says the following:
lather
rinse
repeat

consider that an infinite loop. You can go ahead and replace the repeat with continue, they mean the same thing. The difference is, you can put a continue wherever you want, and it can be conditional, like this:

lather
rinse
if you still have crabs: repeat (continue, in this example)

Or, let's add another condition

lather
if house on fire: get out of shower (the equivalent of break)
rinse
if you still have crabs: repeat

Make a little more sense this way?
I will keep that list up to date at all times. Check often for new materials!

Reply

RE: Break and Continue #4
I understand the principle and use for it, but until I am able to compile the code into a script I don't have a use for the knowledge. That's what I meant by one my head lol.

Once I see it being used first hand in something I write it'll be in focus for me.
[Image: THqqjY5.gif]

Reply

RE: Break and Continue #5
Thanks Biggrin

Reply

RE: Break and Continue #6
Thank you so much, keep it up, I have just a question:
What (break) will do, if we used it inside two loops:

Code:
while(true){

while(true){
break;
}

}

And what if it was like this:

Code:
for(){

        while(true){
                  break;
        }

}

Thank you Wink
(This post was last modified: 10-10-2017, 08:17 PM by Mr.Kurd.)

Reply

RE: Break and Continue #7
(10-10-2017, 08:16 PM)Mr.Kurd Wrote: Thank you so much, keep it up, I have just a question:
What (break) will do, if we used it inside two loops:

Code:
while(true){

while(true){
break;
}

}

And what if it was like this:

Code:
for(){

        while(true){
                  break;
        }

}

Thank you Wink

That will function the same way, but it looks terrible. Everybody has their own style, but I find that people that use that style generally have a lot of issues with their code unless they're really good.
I will keep that list up to date at all times. Check often for new materials!

Reply

RE: Break and Continue #8
(10-11-2017, 04:13 AM)phyrrus9 Wrote:
(10-10-2017, 08:16 PM)Mr.Kurd Wrote: Thank you so much, keep it up, I have just a question:
What (break) will do, if we used it inside two loops:

Code:
while(true){

while(true){
break;
}

}

And what if it was like this:

Code:
for(){

        while(true){
                  break;
        }

}

Thank you Wink

That will function the same way, but it looks terrible. Everybody has their own style, but I find that people that use that style generally have a lot of issues with their code unless they're really good.

My question is: Will the break stop both loops?!
I use Java more so don't know styling C Biggrin

Reply

RE: Break and Continue #9
(10-11-2017, 06:19 AM)Mr.Kurd Wrote:
(10-11-2017, 04:13 AM)phyrrus9 Wrote:
(10-10-2017, 08:16 PM)Mr.Kurd Wrote: Thank you so much, keep it up, I have just a question:
What (break) will do, if we used it inside two loops:

Code:
while(true){

while(true){
break;
}

}

And what if it was like this:

Code:
for(){

        while(true){
                  break;
        }

}

Thank you Wink

That will function the same way, but it looks terrible. Everybody has their own style, but I find that people that use that style generally have a lot of issues with their code unless they're really good.

My question is: Will the break stop both loops?!
I use Java more so don't know styling C Biggrin

No. It only jumps out one level.

Code:
while (1)
{
    while (1)
    {
        break;
        //this code won't get executed
    }
    // the break puts you here
}

As for styling, C and java use the same constructs. If you write your code like that you WILL have problems, no matter what language.
I will keep that list up to date at all times. Check often for new materials!

[+] 1 user Likes phyrrus9's post
Reply

RE: Break and Continue #10
(10-11-2017, 11:25 AM)phyrrus9 Wrote:
(10-11-2017, 06:19 AM)Mr.Kurd Wrote:
(10-11-2017, 04:13 AM)phyrrus9 Wrote: That will function the same way, but it looks terrible. Everybody has their own style, but I find that people that use that style generally have a lot of issues with their code unless they're really good.

My question is: Will the break stop both loops?!
I use Java more so don't know styling C Biggrin

No. It only jumps out one level.

Code:
while (1)
{
    while (1)
    {
        break;
        //this code won't get executed
    }
    // the break puts you here
}

As for styling, C and java use the same constructs. If you write your code like that you WILL have problems, no matter what language.
Thank you so much bro Smile

Reply






Users browsing this thread: 1 Guest(s)