I foudn this tut for looping within c++, gives you a step by step guide as to how to use them.
I put them into Word so it can be a print out if need be
I foudn this tut for looping within c++, gives you a step by step guide as to how to use them.
I put them into Word so it can be a print out if need be
seems OK, but NEVER use goto. Cant see why it took a fair few pages to explain loops, thats really really basic program control.
Yeah i agree mate, i wanted to build up from basic programs to programs which have a real use if you get what i mean?
For Novice users they can start off at the bottom and get use tio programming basic programs then work there way up when used to all the functions and lines of code etc.
I used goto when first started.
cheers
Nik
i used to do basic (as in the language called that) when i was about 12 and used goto all the time
not anymore! very very very bad habit to get itnto - proper code right from the start!
The only exception I make to the use of goto's is for jumping to the end of a function block for tidying up, e.g.:
Code:int foo() { int status = 0; FILE* pfOutput1 = NULL; FILE* pfOutput2 = NULL; // Open file 1 if( status = bar( &pfOutput1 ) ) { goto EndOfFunction; } // Open file 2 if( status = bar( &pfOutput2 ) ) { goto EndOfFunction; } EndOfFunction: // Opened file 1? if( pfOutput1 ) { fclose( pfOutput1 ); } // Opened file 2? if( pfOutput2 ) { fclose( pfOutput2 ); } }
you can always write the code, so that the Go to's are not necessary though! which I personally think is much more readable than using Go to's
I never said you couldn't, the above was just a simple example showing my personal preference to a case where I think goto's are acceptable especially in C.you can always write the code, so that the Go to's are not necessary though!
I know what ya meant, Goto's a certainly a personal preference. I myself, think they are bad to use in any way, and promote bad structuring of code. I know people (inc. yourself) that would argue the point, hence the reason I say personal preference...
I know Sun considered taking GoTo out of their Java language, and I am pretty sure that Microsoft have taken GoTo their dotNet language too..
each to their own I say... although the staff that work with me on projects would be shot for using Goto's
Can you use a try / catch / finally block in C++ ?
This is the nicer way to do exactly what you have done using goto's above but in Java (well almost Java )
public int foo() {
int status = 0;
File pfOutput1 = null;
File pfOutput2 = null;
try {}
// Open file 1
if (status == bar( &pfOutput1)) {
Throw new WereGonnaCatchThisException("...");}// Open file 2} catch (WereGonnaCatchThisException e) {
if ( status = bar( &pfOutput2 )) {
Throw new WereGonnaCatchThisException("...");}
// Opened file 1?
if( pfOutput1 ) {
fclose( pfOutput1 );}// Opened file 2?} finally {
if( pfOutput2 ) {
fclose( pfOutput2 );}
// OR if another exception is thrown and you still want to}
// ensure things are closed then close the files in here...
Last edited by salvadorescobar; 12th February 2004 at 06:29 PM.
'If we aren't meant to eat animals, then why are they made out of meat?'
Anon.
try/catch is expensive and finally is not available in all C++ implementations.
If the 'IF' line is not valid, none of it's following contents are read, so you could this to contain your 'Jump' code
Procode ..
I wouldnt use try - except blocks either, way too expensive.
And I definitely do not agree with using goto in C++, bad style, you may as well go back to C.
I would make the function EndOfFunction return a value, and then return that:
int foo()
{
int status = 0;
FILE* pfOutput1 = NULL;
FILE* pfOutput2 = NULL;
// Open file 1
if( status = bar( &pfOutput1 ) )
{
return EndOfFunction;
}
// Open file 2
if( status = bar( &pfOutput2 ) )
{
return EndOfFunction;
}
Goto's are perfectly fine as long as the don't obfuscate the code. If you use a hammer incorrectly it will hurt, but that is not a reason not to use one.
If they were that bad they wouldn't be included in the language!!!
Like RhinoBanga and others I use it to jump to the end of a function. I mostly use ODBC SDK and so exceptions aren't available, but you have to ensure that the statement handle is de-allocated.
Social Networking Bookmarks