fork(1) download
  1. #include <stdio.h>
  2. /*
  3. Goto example
  4. */
  5. #define true 1
  6. #define false 0
  7. #define bool int
  8.  
  9. int main(void) {
  10.  
  11. for( int i = 0; i < 10; ++i ){
  12. for( int j = 0; j < 10; ++j ){
  13. if( j == 3 ) goto exit_two_loops;
  14. printf("%d + %d = %d\n", i, j, i+j ); /* just fo example */
  15. }
  16. }
  17. exit_two_loops:
  18.  
  19. { /* of course we do not need this to be visible outside */
  20. bool we_should_exit_i = false;
  21. for( int i = 0; i < 10; ++i ){
  22. for( int j = 0; j < 10; ++j ){
  23. if( j == 3 ) {
  24. we_should_exit_i = true;
  25. /* note that in real program it will not be that simple */
  26. break;
  27. }
  28. printf("%d + %d = %d\n", i, j, i+j ); /* just for example */
  29. }
  30. if( we_should_exit_i ) break;
  31. }
  32. }
  33.  
  34. puts( "Why do we need 'goto'? We don't. Goto is bad, do not use it." );
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
0 + 0 = 0
0 + 1 = 1
0 + 2 = 2
0 + 0 = 0
0 + 1 = 1
0 + 2 = 2
Why do we need 'goto'? We don't. Goto is bad, do not use it.