Program flow

This is a discussion on Program flow within the C++ Programming forums, part of the General Programming Boards category; Is there a better way to after the first if statement go back to the start of the second while ...

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

    Program flow

    Is there a better way to after the first if statement go back to the start of the second while loop and not continue in the while loop but not to come out of it either?

    I did it by adding a second while loop outside of the while loop.

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    
        while ( 1 )
        {
            while ( 2 )
            {
               cout << "1\n";
    
               if ( 1 )
               {
                   cout << "2\n";
                   break;
               }
    
               if ( 1 )
               {
                   cout << "3\n";
                   break;
               }
    
            }
            Sleep(1000);
        }
        return 0;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    well what is your aim though?
    The while loop by definition, will continue to loop 'while' the condition is true.
    You break out of the inner loop but will still be in the outer loop until the condition fails.
    When you say 'not continue in the while loop' do you mean 'drop back to the outer loop but do not have any of its statements executed?

    Youwill have to have further control to avoid infinite loops but based on your example code only, maybe this is what you mean? >

    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
    
    bool do_this = true;
    
        while ( 1 )
        {
            if(do_this)
            {
                while ( 2 )
                {
                   cout << "1\n";
                   
                   if ( 1 )
                   {
                       cout << "2\n";
                       do_this = false;
                       break;
                   }
    
                   if ( 1 )
                   {
                       cout << "3\n";
                       do_this = false;
                       break;
                   }
                } 
                Sleep(1000);          
            }        
        }
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Thanks for the help!

    That's not exactly what i want, ill try to explain it again.

    Lets say in that situation below i would like to stay in the while loop but im gonna come out
    because of the break; in "if".
    In the same time if it entered the first "if" loop i dont want to check what is after it just wanna go back and start all over.

    So i would need something else that break; but i wanna avoid goto;
    but maybe its the case to use goto.

    Code:
    while ( 2 )
            {
               cout << "1\n";
    
               if ( 1 )
               {
                   cout << "2\n";
                   break;
               }
    
               if ( 1 )
               {
                   cout << "3\n";
                   break;
               }
    
            }
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    [sorry, its still a bit tricky to understand..., it seems essentially you are talking about a mainloop 'engine' that keeps running, and then an inner loop that needs to be repeatedly used
    in such a case you could use the outer loop to always reset the values of the inner loop, thus every time you go back in its like starting over, but maybe try another description of problem!

    Code:
    //initial outer loop vars
    //initial inner loop vars
    
    outer
    {
        inner
        {
           //do stuff
           //exit condition
        }
    
       //reset inner loop vars as neccesary
       //+ rem: reset inner loop exit condition to allow access again.
    
    //outer exit condition
    
    }
    Last edited by rogster001; 02-11-2010 at 10:17 AM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Your posts are a bit confusing, but I think this is what you're asking for?
    Code:
    while (2)
    {
    	cout << "1\n";
    
    	if ( 1 )
    	{
    	cout << "2\n";
    	continue;
    	}
    
    	if ( 1 )
    	{
    	cout << "3\n";
    	continue;
    	}
    }

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    812
    Ok, sorry for not being clear enough and yes its "continue;" what i need exactly.

    Thanks very much Mike and Rogster!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Replies: 2
    Last Post: 05-10-2002, 05:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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