Is there a way to write such program without using goto? If so, how?
-
7Anything is possible. – super Feb 12 '18 at 15:30
-
1you just got to do it – L_Church Feb 12 '18 at 15:31
-
_Is there a way to write such program without using goto?_ Yes. _If so, how?_ Code. – Eljay Feb 12 '18 at 15:31
-
_@Philip_ You could at least have spent some efforts in crafting a markup text representation of that flowchart. – Feb 12 '18 at 15:34
-
13That spray of downvotes and the close vote reason baffle me. This is a clear question, to which the answer is actually *no*. But I guess simply mentioning `goto` has that effect. – Quentin Feb 12 '18 at 15:41
-
Yes you can do anything without `goto`. Have you tried `if { } else { }` or even better `switch { case }` How about `do { } while { }`? – Justin Randall Feb 12 '18 at 15:41
-
You may not really have that exact control flow graph depending on how you model it and whether you allow yourself to perform simplifications or not (or perhaps if you "cheat" and use things like longjmp and setjmp instead of goto), but you obviously can have something which behaves basically the same ( https://ideone.com/EtKyIw ) which makes the question of no "real practical interest". – Caninonos Feb 12 '18 at 15:50
-
Is it really unclear what OP is asking here? They could articulate it more, but we get the question. – Justin Randall Feb 12 '18 at 15:51
-
4I agree with Quentin with regards to closing the question. Not sure I agree that the answer is no - it's going to be messy both WITH and WITHOUT goto, and depending on how much you think goto is "bad", the alternative may be better. – Mats Petersson Feb 12 '18 at 15:51
-
1@Quentin I think the problem is mostly that red boxes don't make a program which makes "such a program" an ill-formed phrase which makes the whole question unanswerable. If there was some code instead of boxes there might be a reasonable answer. We can't even begin to argue about the merits and issues of `goto` because we have no basis to compare. – nwp Feb 12 '18 at 15:52
-
1@Quentin Note that two of the votes were for "Too Broad" as I hadn't noticed the complexity of the question. I thought the question was asking about embedded loops. And it seemed to me that asking "how do I write loops?" would be off topic. Though, the answer to the question is "yes", it's possible to write this control flow without `goto` with some effort. – François Andrieux Feb 12 '18 at 15:54
-
@nwp labeling the flowchart would help indeed, but the question essentially asks *how* to translate that into code. – Quentin Feb 12 '18 at 15:54
-
@FrançoisAndrieux to me the only viable meaning of this question is "is there a combination of high-level control structures that can naturally describe this flowchart". Since OP presents us with one of the canonical examples (crossed loops) where the answer is "no", it seems reasonable that this is indeed the question. – Quentin Feb 12 '18 at 15:59
-
How do we know this flowchart was even written by a programmer, or had a computer program for it in mind? It could be my sister's flowchart of baking a cake for all we know. – PaulMcKenzie Feb 12 '18 at 16:13
-
2Honestly this looks like a duplicate (slight variant) of [this question](https://stackoverflow.com/questions/21582270/is-it-possible-to-write-this-flowchart-into-code-without-variables-and-goto) and the accepted answer shows how to do it with recursion. So the answer is _yes_ it can be done without `goto`. – Justin Randall Feb 12 '18 at 16:14
-
I used to have a job writing Haskell full time. That language's biggest biggest strength was that you could use it to sanely write those kinds of control flows. – hegel5000 Feb 12 '18 at 16:18
-
[...](http://coliru.stacked-crooked.com/a/f9c5e543721a7a8e) – jaggedSpire Feb 12 '18 at 16:27
-
Well... technically your compiled code would have something equivalent to a 'goto'. But trying to code without any for of jumping would basically be stupid. State machine is clearly the solution to this problem. – thecoshman Feb 12 '18 at 17:22
-
1@JustinRandall **eerily similar**. I think I just did someone's homework assignment. :( – Drew Dormann Feb 12 '18 at 19:06
-
Of course a goto isn't necessary. Write it in Intercal, and use a couple of COMEFROM statements. – Jerry Coffin Feb 12 '18 at 20:43
-
both conditions could loop to first point, but second would have a parameter to tell to not execute the first block – lunadir Nov 24 '22 at 09:19
3 Answers
"Is there a way to write such program without using goto?"
-- Philip Moreira
Yes. Technically speaking, C++ is flexible enough that you can forbid pretty much any one construct and still be able to implement what you want, including goto
.
The "how" part is a bit uninteresting, so let's just say you'd do that by taking two loops, using additional flag variables in one of the conditions so it doesn't behave like a loop anymore, and carefully use if
to end up with something that meets the specification.
"Yes, but is there a way to naturally model this flowchart with only high-level flow control?"
-- Helpful voice-over
No.
This is actually one of the canonical cases where C++'s high-level constructs are not enough. All of them are designed around a concept of nested scopes (exception handling can cut a scope short, but that's it). This flowchart features two crossed loops, with just enough operations in-between that you can't just shuffle them around to get nested loops.
You have two choices:
Just use
goto
. Document it, make the label explicit, and apply other usual good practices.Try to stick to the "no
goto
" rule to hell and forth, bending other control structures out of shape until you have effectively emulated agoto
, with a side serving of rogue booleans and tricky conditions.
The second solution might end up more legible than the first (never say "never", they say), but you definitely shouldn't discard the first one before trying.
For reference, here is how you would translate your flowchart (actions and conditions numbered from their order in the flowchart):
action_one;
properlyNamedLabel:
action_two;
do {
action_three;
if(condition_one)
goto properlyNamedLabel;
action_four;
} while(condition_two);
action_five;

- 62,093
- 7
- 131
- 191
This is one of many potential solutions, which your compiler could make as efficient as using goto
s.
One branch is handled in a do/while condition. The other branch is handled by a return
.
bool func();
int main()
{
statement1();
while( func() ) {}
statement5();
}
bool func()
{
statement2();
do
{
statement3();
if ( condition1() )
{
return true;
}
statement4();
} while( condition2() )
return false;
}
You may notice by viewing the code that the logic is rather complex.
In a real-world program, this may be a sign that the program logic needs to be reviewed.

- 59,987
- 13
- 123
- 180
I don't think anyone would ever attempt to do this flow chart "as is" - but with some modifications it could be made.
The first loop is clearly a do {} while()
. Let us pull that out into a function for now to make our life easy.
The second loop wants us to jump into the middle of the do while. Well, rather than do this with goto's; it's nicer for us to call our function; with a flag to not do the first thing.
Note that I have used the 2nd task in the flow chart as my 1st; since it's the first in a loop.
As such you end up with something like
void myFirstLoopFunc(bool doFirstTask) {
do {
if (doFirstTask)
firstTask();
doFirstTask = true;
secondTask();
}
while(firstTest());
}
void mySecondLoopFunc() {
bool doFirstThing = true;
do {
myFirstLoopFunc(doFirstThing);
thirdTask();
doFirstThing = false;
} while (secondTest());
}
One would then call mySecondLoopFunc to start the chain.

- 6,941
- 3
- 21
- 30