Close

Results 1 to 13 of 13

Thread: c++ looping tut

  1. #1
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,411
    Thanks
    558
    Thanked:        148
    Karma Level
    877

    Default c++ looping tut

    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

  2. #2
    DF VIP Member WTD's Avatar
    Join Date
    Mar 2001
    Location
    Tokyo, Japan
    Posts
    3,256
    Thanks
    0
    Thanked:        0
    Karma Level
    470

    Default Re: c++ looping tut

    seems OK, but NEVER use goto. Cant see why it took a fair few pages to explain loops, thats really really basic program control.

  3. #3
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,411
    Thanks
    558
    Thanked:        148
    Karma Level
    877

    Default Re: c++ looping tut

    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

  4. #4
    Argyll's Apprentice TwoPlAnKs's Avatar
    Join Date
    May 2003
    Location
    Aberdeenshire
    Posts
    5,191
    Thanks
    0
    Thanked:        0
    Karma Level
    598

    Default Re: c++ looping tut

    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!

  5. #5
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    367

    Default Re: c++ looping tut

    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 );
        }
    
    }

  6. #6
    DF VIP Member graham.edmon's Avatar
    Join Date
    May 2001
    Location
    Edinburgh
    Posts
    263
    Thanks
    0
    Thanked:        0
    Karma Level
    268

    Default Re: c++ looping tut

    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

  7. #7
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    367

    Default Re: c++ looping tut

    you can always write the code, so that the Go to's are not necessary though!
    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.

  8. #8
    DF VIP Member graham.edmon's Avatar
    Join Date
    May 2001
    Location
    Edinburgh
    Posts
    263
    Thanks
    0
    Thanked:        0
    Karma Level
    268

    Default Re: c++ looping tut

    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

  9. #9
    DF VIP Member salvadorescobar's Avatar
    Join Date
    Dec 2000
    Location
    Huddersfield
    Posts
    951
    Thanks
    0
    Thanked:        0
    Karma Level
    324

    Default Re: c++ looping tut

    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
    if ( status = bar( &pfOutput2 )) {
    Throw new WereGonnaCatchThisException("...");
    }
    } catch (WereGonnaCatchThisException e) {
    // Opened file 1?
    if( pfOutput1 ) {
    fclose( pfOutput1 );
    }
    // Opened file 2?
    if( pfOutput2 ) {
    fclose( pfOutput2 );
    }
    } finally {
    // 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.

  10. #10
    DF VIP Member
    RhinoBanga's Avatar
    Join Date
    Nov 2000
    Location
    127.0.0.1
    Posts
    1,780
    Thanks
    20
    Thanked:        13
    Karma Level
    367

    Default Re: c++ looping tut

    try/catch is expensive and finally is not available in all C++ implementations.

  11. #11
    DF VIP Member procode's Avatar
    Join Date
    Jan 2002
    Location
    North of England
    Posts
    335
    Thanks
    0
    Thanked:        0
    Karma Level
    263

    Default Re: c++ looping tut

    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 ..

  12. #12
    DF VIP Member nitelife's Avatar
    Join Date
    Nov 2002
    Location
    Reading, Berks
    Posts
    1,170
    Thanks
    115
    Thanked:        13
    Karma Level
    315

    Default Re: c++ looping tut

    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;
    }

  13. #13
    DF MaSter Smegma's Avatar
    Join Date
    Jan 2001
    Posts
    57
    Thanks
    0
    Thanked:        0
    Karma Level
    263

    Default Re: c++ looping tut

    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.

Similar Threads

  1. Replies: 6
    Last Post: 21st March 2013, 06:31 AM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •