Program flow

This is a discussion on Program flow within the C++ Programming forums, part of the General Programming Boards category; If i understand well the program flow in a while(true) statement, it goes until the end and then it comes ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    856

    Program flow

    If i understand well the program flow in a while(true) statement, it goes until the end and then it comes back to the beginning.
    What if i want is to come back after the first if() statement and to continue if it gets in the else().
    Is this the case for goto() ?

    Code:
     while(true)
    {
        // do something
           
        //FIRST IF()
        if(condition)
        {
           //go back to while(true)
        }
        else
        {
             //continue
        }
     //SECOND IF()
        if(condition)
        {
           
        }
        else
        {
             
        }
    }
    Last edited by Ducky; 04-20-2009 at 07:09 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Quote Originally Posted by Ducky
    What if i want it to come back after the first if() statement and that it continues it it gets in the else().
    What do you mean? I think you made at least one typographical error that makes it difficult to understand what you mean. That said, I have the feeling that you are looking to use the continue keyword, or to re-structure the loop entirely.

    Quote Originally Posted by Ducky
    Is this the case for goto() ?
    goto is not a function as it is a keyword.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    Yes, im sorry for the typo.

    What i meant is it to go back to the beginning of the while(true) loop if it enters the first if() statement and to continue if it enters else().
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,817
    Then it should be along these lines:
    Code:
    while (true)
    {
        // do something
    
        //FIRST IF()
        if (condition1)
        {
            continue;
        }
    
        //SECOND IF()
        if (condition2)
        {
            // ...
        }
        else
        {
            // ...
        }
    }
    or possibly more clearly:
    Code:
    while (true)
    {
        // do something
    
        //FIRST IF()
        if (!condition1)
        {
            //SECOND IF()
            if (condition2)
            {
                // ...
            }
            else
            {
                // ...
            }
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    Thank you!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21