Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 5th, 2009, 02:41 PM
Registered User
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default P171, continue statement. Broken model?

I understand the meaning and usage of your examples on pages 171 and 172 in reference to the continue statement.

Code:
for (ID = 0; true; ID++)
{
   state = readSensor(ID);

   if (state == true)
   {
      soundAlarm();
      callFireDepartment();
      continue;
   }

   if (ID == MAXSENSORS)
   {
      ID = -1; // -1 so increment operator sets it to 0
   }
}
I'm wondering if the continue statement in these examples don't actually break the functioning of the code in a situation where the sensor that is tripped (true) is equal to MAXSENSORS? Wouldn't that allow ID to become a value greater than MAXSENSORS and thus ID will never get set to -1 until the routine is restarted?

In essence is there really a need for the continue statement in this code for anything other than an example of it's usage?

Last edited by jstrudwick; June 5th, 2009 at 02:46 PM.. Reason: Added code snippet
 
Old June 6th, 2009, 09:41 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

You're right. Actually, I think the technical editor on the book also caught this error (I was tempted to say I caught it, but..alas...I didn't). I'm not sure how it slipped through to production. The proper way to fix it is to place a statement just above the continue statement that resets the value of ID.

While the code is clearly wrong, eventually the code would reset itself. That's the good news. The bad news is that it would have to cycle through several billion interations of the loop before it was reset!

Sorry for the error...
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old November 30th, 2010, 08:45 AM
Registered User
 
Join Date: Nov 2010
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts
Default

I actually don't get the point of the continue statement in this example. Especially if you want to fix the problem mentioned by jstrudwick, wouldn't it be much simpler to just leave out the continue statement at all?

I suppose the problem is that it's hard to come up with a good example where the continue statement actually makes sense. Allmost all samples I found have code that skips some values, something like
Code:
for (int i = 1; i <= 10; i++)
{
   if (i == 4)
   {
     continue;
   }
   Console.WriteLine(i);
}
That could easily be rewritten to
Code:
for (int i = 1; i <= 10; i++)
{
   if (i != 4)
   {
     Console.WriteLine(i);
   }
}
Off course there is one difference, the indentation of the Console.WriteLine(i); So maybe, the statement is usefull for preventing deep nesting? Or is it more something that's up to the programmer. Some might find it makes more sense to explicitely state that you want to continue with the next iteration and skip the rest of the loop.
 
Old November 30th, 2010, 03:32 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default The continue statement

Almost any example I can think of for the continue statement can be rewritten without it, which probably explains why you rarely see it used in "real" code. Indeed, I have a sneaky suspicion that the compiler often reorganizes the code so it behaves like it's not there anyway. Perhaps one argument for the continue statement is that it can document the intent of the code in a manner that might be more clear in some cases. If it is true that the compiler optimizes the (more verbose) code using the continue away but the code remains better documented, perhaps the continue should be used.

If anyone has a really good example of the continue statement, I'd like to see it.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
continue dayang Classic ASP Basics 1 April 29th, 2008 11:22 PM
How to continue to wite to a text file Hoang Excel VBA 2 March 10th, 2008 05:03 PM
How to continue to next new excel worksheet christine_wmw Classic ASP Basics 0 January 24th, 2007 02:54 AM
continue topic: form lock down problem flyfish Access 6 March 30th, 2005 11:31 AM
break & continue problem. gredata C# 1 December 21st, 2004 05:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.