-
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;
}
-
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;
}
-
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;
}
}
-
[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
}
-
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;
}
}
-
Ok, sorry for not being clear enough and yes its "continue;" what i need exactly.
Thanks very much Mike and Rogster!