break statement

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

How could I break out of the while loop within the for/if loop below

while (intCurCategory < intTotalCategories)
{
for (intPageIter = 0;intPageIter< ITEMS_PER_PAGE;intPageIter++)
{
if (intPagesDisplayed>ITEMS_PER_PAGE)
break; //trying to break out of the while loop here!
intPagesDisplayed++
}

}
 
bool done = false;

while( (intCurCategory < intTotalCategories) && !done )
{
for (intPageIter = 0;intPageIter< ITEMS_PER_PAGE;intPageIter++)
{
if (intPagesDisplayed>ITEMS_PER_PAGE)
done = true;
break; //trying to break out of the while loop here!
intPagesDisplayed++
}

}
 
whilst I am no expert, let me make a few comments.
You could use the goto statement (with a label to jump to) however this is
usually necessary when the code is bad.

From your code snippet it is very hard to see exactly what you are trying to
do but the chances are you could do a secondary && test within the while
loop -either moving the if statement test itself, or if that is not possible
setting a boolean flag
e.g.

bool loopFlag = true;
while (intCurCategory < intTotalCategories && loopFlag)
{
for (intPageIter = 0;intPageIter< ITEMS_PER_PAGE;intPageIter++)
{
if (intPagesDisplayed>ITEMS_PER_PAGE)
loopFlag = false;
break; //this will break the for loop
intPagesDisplayed++
}
}

The other thing that strikes me about the code is the god awful naming of
the integers. Ive been banging on for a while in the CSharp newsgroup about
the lack of a firm standard when naming private variables , controls etc.
And I intend to put this right myself sometime because it's something that I
believe slows down development time cos you are constantly thinking "what
shall I call this". I think the general consensus is that for private
variables you should name them in Hungarian format (as you did) , but you
dont need the "int" in a type safe environment this is extreme overkill in
my opinion (plus is confusing to the eye when a variable declaration could
be "int variableName = value", also the names should be functionally
descriptive (as you did) rather than abbreviated rubbish.
For my form controls however I am currently of the opinion to prefix those
(a la VB format) since it makes my life easier finding them using
intellisense however I have had one expert tell me he doesnt do this either!

I hope some of this helps! Good luck.

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
AHHHHHHHHHHHHHH
NO GOTO PLEASE !!!!!!!!

It is possible............ EVERY GOOD PROGRAMMED WOULD NEVER USE GOTOS....

I used gotos in gwbasic in the msdos days, !!!!!!!! aweful, I used
gotos/jmps in assembly back then.......
in assembly only they are necessary.......

in C they are provided and yet they make the code flow hard to deal with and
dont achieve things any better then using while/loops/fors....

just refer to what Gabriele G. Ponti first wrote, use beark; to get off the
inner loop and a flag to get off the outer one or yet anothe break.
 
I guess you feel quite strongly about this!!

andrea catto' said:
AHHHHHHHHHHHHHH
NO GOTO PLEASE !!!!!!!!

It is possible............ EVERY GOOD PROGRAMMED WOULD NEVER USE GOTOS....

I used gotos in gwbasic in the msdos days, !!!!!!!! aweful, I used
gotos/jmps in assembly back then.......
in assembly only they are necessary.......

in C they are provided and yet they make the code flow hard to deal with and
dont achieve things any better then using while/loops/fors....

just refer to what Gabriele G. Ponti first wrote, use beark; to get off the
inner loop and a flag to get off the outer one or yet anothe break.



trying that
 
why dont you read my goddam post before jumping on your high horse!
i.e. the 2nd sentence which reads "however this is usually necessary when
the code is bad."

and the 3rd line which reads "you could do a secondary && test within the
while loop -either moving the if statement test itself, or if that is not
possible setting a boolean flag"

or even the code example on the 5th line onwards.......


:(

p.s.

I used GOTOs in Texas Intruments 99 days (pre dating DOS by around 10
years) -so what?!
A jump is valid in any language that it has been implemented but should be
used ONLY if it is the last resort AND ideally suited to the task in hand.

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
**** you to begin with, since you started with "goddamn" and high horse.

there are many alternatives to everything, good and bad ones, although the
bad ones should not even be mentioned to people new to a certain subject,
regardless.

good guidelines whould only be taught.

and I'd reenforce the first line to you.
whatever also..
 
Mark Broadbent said:
whilst I am no expert, let me make a few comments.
You could use the goto statement (with a label to jump to) however this is
usually necessary when the code is bad.

From your code snippet it is very hard to see exactly what you are trying
to
do but the chances are you could do a secondary && test within the while
loop -either moving the if statement test itself, or if that is not
possible
setting a boolean flag
e.g.

bool loopFlag = true;
while (intCurCategory < intTotalCategories && loopFlag)
{
for (intPageIter = 0;intPageIter< ITEMS_PER_PAGE;intPageIter++)
{
if (intPagesDisplayed>ITEMS_PER_PAGE)
loopFlag = false;
break; //this will break the for loop
intPagesDisplayed++
}
}

The other thing that strikes me about the code is the god awful naming of
the integers. Ive been banging on for a while in the CSharp newsgroup
about
the lack of a firm standard when naming private variables , controls etc.
And I intend to put this right myself sometime because it's something that
I
believe slows down development time cos you are constantly thinking "what
shall I call this". I think the general consensus is that for private
variables you should name them in Hungarian format (as you did) , but you

I wouldn't suggest talking consensus on this issue. Its actually probably
pretty split. I for one hate hungarian and I'm not alone. Its a bad standard
and, IMHO, it turns good code into nearly unreadable rubbish. Well named
variables will pretty much always remove the need for any prefixes, be they
scope or type. We have probably argued the issue before, I've been involved
in a couple of the naming convention debates, but I still think its
irresponsible to decide what the consensus is off the cuff. Let the OP look
around and find all of debate on the issue, if he cares to do so, and let
him draw his own conclusions. Beyond that, let him write his code as he
wishes, he may think your variable naming is godawful as well.

For my form controls however I am currently of the opinion to prefix those
(a la VB format) since it makes my life easier finding them using
intellisense however I have had one expert tell me he doesnt do this
either!

I still prefer well named variables, although I do do this oftentimes with
textboxes. I prefer
customerNameTextBox to txtCustomerName, however they both have a major
flaw...they convey incorrect information if the text box is changed to a
combo box. In some cases it isn't a big deal, but the hungarian notation
bites you if you don't change it. txtCustomerName better be a text box, not
a combo box or your variable name is wrong. It gets trickier when you throw
in 3rd party controls...treeCustomers may be a standard TreeView, or it may
be DevExpresses TreeList, or it may be something else entirely. The name
treeCustomers implies TreeView, but often enough its wrong. This shows a
major problem with hungarian, sometimes the variable names imply both a type
and a use and cross eachother up doing it. I've been trying to find a
better naming scheme for this that increases freedom and reduces the
confusion, but I havn't so far. You always seem to add the name of the base
control type to the variable. Hopefully refactoring will solve what naming
conventions can't seem to, in this case.
 
andrea catto' said:
AHHHHHHHHHHHHHH
NO GOTO PLEASE !!!!!!!!

It is possible............ EVERY GOOD PROGRAMMED WOULD NEVER USE GOTOS....

No good programmer would ever ignore a tool simply out of bigotry either. It
is there for a purpose, its general uses are very limited but arbitrarily
deciding that you'll never use it, let alone that no one else should ever
use it, is as foolish as using it instead of any other looping construct.
 
yeah your right. My mistake -I meant use camel casing for locals (not
Hungarian -getting confused between the two!), currently use Hungarian for
controls. As i said in my argument the prefix aint necessary in a type safe
environment.
End of the day as you suggest, one mans muck is another mans treasure, so as
long as a standard works for one person it is better than no standard at
all.

However this has been bothering me for some time now because in the .net
days of interoperation between code, it makes sense that there should be a
complete standard to simplify the design and development process of code
between .NET programmers of today -and lets hope in 10 years time.

Must admit Im not completely convinced with Hungarian for controls and your
point is right on the money, the only thing that is swaying it at present is
that it is easier to find them in the designer using intellisense.


*Patrick please see one mans view on this link -it covers most things (and
is obviously open to modification to suit your ideals)
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=336#10
--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
However this has been bothering me for some time now because in the .net
days of interoperation between code, it makes sense that there should be a
complete standard to simplify the design and development process of code
between .NET programmers of today -and lets hope in 10 years time.

I disagree, interoperation between code has nothing to do with private
methods or variables, thats why they are private.
 
you are misunderstanding my point. Yes interoperation between code involves
only public members -but because of the ability to interoperate between
languages with ease there is a higher chance that their is going to be a
larger base of source code in multiple languages that one programmer will
have to manage hence (in my opinion) a good reason why creating a global
..net public/private standard would be a good idea. Additionally new
programmers picking up a language wouldnt spend too much time thinking about
how to name their variables, methods and the like and more time on learning
their new language. Parameters are also exposed by the designer and
reflection meaning its a good idea to follow a convention for them.

We dont have to agree on this subject, but I can tell you as far as I am
concerned I want to write code classes with an understandable implementation
naming so Joe Bloggs can pick it up quite easily -but more importantly that
I can understand Joe Bloggs' implementation. I can only see this not being
an issue IF we never need to work on each others code, which is unlikely in
most scenarios.


--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
Mark Broadbent said:
We dont have to agree on this subject, but I can tell you as far as I am
concerned I want to write code classes with an understandable implementation
naming so Joe Bloggs can pick it up quite easily -but more importantly that
I can understand Joe Bloggs' implementation. I can only see this not being
an issue IF we never need to work on each others code, which is unlikely in
most scenarios.

On the contrary - I'd say most of the time you *don't* need to work
with other people's source code, unless they're on the same team as
you. How many 3rd party libraries have you had to closely examine the
source code for?

Note that for it to be understandable for the odd bug fix or whatever
you don't need to be using the same naming conventions - it would be a
pain if you wanted to write a whole heap of extra functionality within
the library, or actually collaborate on the project with Joe Bloggs,
but that's a different matter, and will happen much more rarely.
 
I'm still unconvinced that it would'nt be more beneficial to something
rather than nothing since possible scenarios can exist, however infrequent.
Every now and again I see (what I think are) inconsistancies in the way
several of the Class members parameters have been named (dont mean casing)
and it just irritates me a tiny bit.

I do seem to be in the minority on this issue so I'll see if my mind changes
over time.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
Or just repeat the if statement just after the for loop. But you may want to
rethink your loop and go by a display item pulling in categories as needed
instead of iterating categories and breaking when your page is full.

Jerry
 
Back
Top