Using "break" and "continue" in "do" and "

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 520 Views - Last Post: 03 June 2013 - 04:08 AM Rate Topic: -----

#1 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:19 PM

I'm beginning to understand how loops work, but I seem to have a bit of a problem understanding some logic when it comes to using "break" and "continue" in "do" and "do while" loops.

The book I am reading is having me do a couple of exercises on loops. Here is the first bit of code form the first exercise:

            Console.WriteLine("Start!");

            for (int i = 1; i < 10; i++)
            {
                if (i == 5)
                    break;

                Console.WriteLine("Pass {0} in the loop!", i);
            } 

            Console.WriteLine("End!");
            Console.ReadLine();


Now, in this loop "i" is assigned a value of 1. Then, as long as "i" remains less than 10, "i" will keep being incremented by 1 until it hits 5. When it hits 5 the loop stops. Basically, you'll see the last message as "Pass 4 in the loop!".

If I substitute "break" for "continue", the output will skip the number 5 but print the rest, all the way to 10. This makes perfect sense to me.

However, the book mentions that you can also use "break" and "continue" with the DO and DO WHILE loops. So, I figured I would give this a shot and see what happens. Here is my code:
            Console.WriteLine("Start!");

            int i = 1;

            do
            {
                if (i == 5)
                    break;

                Console.WriteLine("Pass {0} in the loop!", i++);

            } while (i < 10);

            Console.WriteLine("End!");
            Console.ReadLine(); 


This code does exactly what I expected it to do. It gets to 4 and then stops because IF (i == 5) is now true. However, substituting "break" for "continue" gives me a weird result.

Here is a screenshot of the output:

Posted Image

When the program gets to this point, it hangs and does nothing else.

I expected the program to skip over 5 and continue with the rest of the code, but instead the code gets stuck in an endless loop of checking to see if (i == 5). I verified that the program was stuck in this loop by using the debug feature and hitting F11 until it got to the "continue" portion of code.

I am not quite certain why the "continue" command didn't perform the same function within the DO WHILE loop as it did in the FOR loop. I am trying to understand where the logic went wrong even as I type this out.

I hope that I have provided enough information to get some help with the logic. The book said that the "break" and "continue" commands would work for both loop types and I'm sure that they do. Unfortunately, I think my logic is off a bit and that's why the program gets stuck.

Is This A Good Question/Topic? 0
  • +

Replies To: Using "break" and "continue" in "do" and "

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:23 PM

Easier thing would be to breakout the debugger and step-through the code and see what actually happens.
Was This Post Helpful? 0
  • +
  • -

#3 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:27 PM

View PostAdamSpeight2008, on 02 June 2013 - 02:23 PM, said:

Easier thing would be to breakout the debugger and step-through the code and see what actually happens.


As mentioned at the bottom of the post, I did this.

Quote

I verified that the program was stuck in this loop by using the debug feature and hitting F11 until it got to the "continue" portion of code.


The program goes from continue, down to the WHILE statement, then back up to DO, then down to "continue", then back down to WHILE. This is what happens when I keep hitting F11
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:30 PM

The for loop guarantees the i++ line will be executed. The way the for loop works is that it executes as follows:
for(initialization (1); condition (2); var-modification (4)){
    //code (3)
}



So a continue statement in the body of the loop will cause that iteration of the loop to go straight to step 4, where the i++ statement occurs.

In your do-loop, you have an infinite loop with a continue statement. The do-loop does not guarantee that a variable modification will occur. Thus, continue; skips over the i++ section, so i == 5 for the duration of the program. Hence, an infinite loop.
Was This Post Helpful? 2
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:35 PM

Debuggingisn't just about watch the path of execution, it about thinking about your code.
- This is what it expect the values to be.
- This is what I'm actually getting.
- It's doing this, that and the other.
- So this means it is not doing blah, blah and blah.

If did the above critical thinking, you would have noted that i didn't change its value.

This post has been edited by AdamSpeight2008: 02 June 2013 - 02:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:39 PM

View Postmacosxnerd101, on 02 June 2013 - 02:30 PM, said:

The for loop guarantees the i++ line will be executed. The way the for loop works is that it executes as follows:
for(initialization (1); condition (2); var-modification (4)){
    //code (3)
}



So a continue statement in the body of the loop will cause that iteration of the loop to go straight to step 4, where the i++ statement occurs.

In your do-loop, you have an infinite loop with a continue statement. The do-loop does not guarantee that a variable modification will occur. Thus, continue; skips over the i++ section, so i == 5 for the duration of the program. Hence, an infinite loop.


Makes sense. Thanks for the explanation.

The book I am reading mentions that you can use the "break" and "continue" commands in DO and DO WHILE loops, but I suppose it's assuming the logic is correct. The book never gave me any examples of it's usage, so I figured I would experiment to see what happens if I implemented the "break" and "continue" commands myself.

This is how I learn things. Experimentation is a key element to learning something new.

And yes, you are correct, the DO loop doesn't guarantee that the modification will occur and the book actually mentions this right from the beginning (something I forgot). It's going to be essential that I remember this when using loops as these kinds of mistakes can get frustrating.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 02:43 PM

Glad I could help!
Was This Post Helpful? 1
  • +
  • -

#8 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 04:07 PM

View PostAdamSpeight2008, on 02 June 2013 - 02:35 PM, said:

Debuggingisn't just about watch the path of execution, it about thinking about your code.
- This is what it expect the values to be.
- This is what I'm actually getting.
- It's doing this, that and the other.
- So this means it is not doing blah, blah and blah.

If did the above critical thinking, you would have noted that i didn't change its value.


This is why I need to work on my forum communication skills. I didn't explain my issue very well.

Let me try to be more specific...

While debugging, I noticed that the program was stuck in a loop (for the reason which you explained - 'i' was not being incremented by 1). What I didn't understand was the fact that if I used the FOR loop instead of the DO WHILE loop, 'i' would be incremented by 1 and the program would continue, not hang (as it did with the DO WHILE loop).

My question was really about the "continue" command being used in a DO WHILE loop, and why the variable wasn't being incremented as expected (as with the FOR loop). Yes, the program was stuck in an infinite loop because 'i' wasn't being incremented. Why it wasn't being incremented was the question. I couldn't grasp why the "continue" command would not allow the variable to increment as I expected it to.

Does that make more sense? It probably doesn't and I butchered it even more. Well, I tried.

I'm very bad at explaining things and it's something I am actively working on.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 04:14 PM

You could also brush up on your researching skills.

I've just googled MSDN vb.net continue and the first result is the documentation. (MSDN: Continue)

edit: And I also need read posts more slowly.

MSDN C# continue (MSDN: Continue)

Maybe less than 2 minutes I have informative documentation to read.

This post has been edited by AdamSpeight2008: 02 June 2013 - 04:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 04:33 PM

Ah, so on top of not being able to think critically about my code, I suck at researching :lol:

Well then, I can see that I'm just not ready for these forums quite yet. I'll see myself out...
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 04:37 PM

Quote

Well then, I can see that I'm just not ready for these forums quite yet. I'll see myself out...

These are all skills we really don't have in sufficient supply when starting programming. I was somewhat in the same boat. Your question was a valid one though. The thinking critically part will come with practice. The researching part will get better as you learn what terms to search for. :)
Was This Post Helpful? 1
  • +
  • -

#12 Semus  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 126
  • Joined: 27-May 13

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 06:19 PM

View Postmacosxnerd101, on 02 June 2013 - 04:37 PM, said:

Quote

Well then, I can see that I'm just not ready for these forums quite yet. I'll see myself out...

These are all skills we really don't have in sufficient supply when starting programming. I was somewhat in the same boat. Your question was a valid one though. The thinking critically part will come with practice. The researching part will get better as you learn what terms to search for. :)/>


I've only been doing this for about a month. I had never touched a single line of code in my entire life (38 years old now). It's going to be a very long (never ending) journey. I don't have the advantage of taking programming courses or even having a teacher at my disposal. Still, I can usually pick things up rather quickly. With programming, though, it's going to take me some time to get used to all of the ins and outs of not only the language (or syntax), but also the logic behind what makes your code do what it's supposed to do.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,263
  • Joined: 27-December 08

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 06:37 PM

Many here have picked up programming without the benefit of a classroom. You are not alone. You'll get there. Just keep plugging along. :)
Was This Post Helpful? 0
  • +
  • -

#14 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 07:23 PM

I just want to mention that I'm not a huge fan of break and continue. That's not to say I haven't used them before, but a lot of people consider them "bad practice" because (A)They make the code harder to read and (B)They're basically thinly veiled Gotos.

So my suggestion is you shouldn't use them. Especially when you're first learning. The only time I ever found them truely useful was when I was trying to construct a Prime number generator without using the Sieve of Erasosthenes (you know... just for fun) and I was able to use breaks and continues to squeeze every last ounce of speed out of it. But unless your'e running numbers through a loop a million times, it's not worth it.
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: Using "break" and "continue" in "do" and "

Posted 02 June 2013 - 08:23 PM

Actually, break and continue are also good for preventing the depth of your indents from getting too deep. As code gets more complex, sometimes having one less level of indent actually makes the code more readable and easier to understand.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2