C++ flow control for jumping return and goto statements tutorial

  • 2020-05-07 20:06:53
  • OfStack

return statement
terminates the execution of the function and returns control over the calling function (or the operating system, if you transfer control from the main function). Resumes execution in the calling function immediately after the call.
grammar


return [expression];

note
The expression clause (if it exists) converts to the type specified in the function declaration, as if initialization 1 were being performed. The conversion from an expression of this type to a function of return type creates a temporary object.

The value of the expression clause returns the calling function. If the expression is omitted, the return value of the function is uncertain. Constructors and destructors and functions of type void cannot specify expressions in return statements. All other types of functions must specify expressions in the return statement.
When the control flow exits a block defined by a closed function, the result is the same as that obtained by executing an return statement without an expression. This is not valid for a function declared as a return value.
A function can contain any number of return statements.
The following example USES an expression with return statement 1 to get the largest of two integers.


// return_statement2.cpp
#include <stdio.h>

int max ( int a, int b )
{
  return ( a > b ? a : b );
}

int main()
{
  int nOne = 5;
  int nTwo = 7;

  printf_s("\n%d is bigger\n", max( nOne, nTwo ));
}

goto statement

The goto statement unconditionally transfers control to the statement marked by the specified identifier.
grammar


goto identifier;

note
The markup statement specified by identifier must be in the current function. All identifier names are members of the internal namespace and therefore do not interfere with other identifiers.
Statement labels are only meaningful for goto statements; In other cases, statement labels are ignored. The label cannot be redeclared.
It is a good programming style to use break, continue, and return statements instead of goto statements whenever possible. However, because the break statement exits only one level of the loop, you may have to use the goto statement to exit a deeply nested loop.

In this example, when i is equal to 3, the goto statement transfers control to the point marked stop.


// goto_statement.cpp
#include <stdio.h>
int main()
{
  int i, j;

  for ( i = 0; i < 10; i++ )
  {
    printf_s( "Outer loop executing. i = %d\n", i );
    for ( j = 0; j < 2; j++ )
    {
      printf_s( " Inner loop executing. j = %d\n", j );
      if ( i == 3 )
        goto stop;
    }
  }

  // This message does not print: 
  printf_s( "Loop exited. i = %d\n", i );

  stop: 
  printf_s( "Jumped to stop. i = %d\n", i );
}

Output:


 Executing an external loop. i = 0
  Executing an internal loop. j = 0
  Executing an internal loop. j = 1
 Executing an external loop. i = 1
  Executing an internal loop. j = 0
  Executing an internal loop. j = 1
 Executing an external loop. i = 2
  Executing an internal loop. j = 0
  Executing an internal loop. j = 1
 Executing an external loop. i = 3
  Executing an internal loop. j = 0
 Jump to stop. i = 3

control transfer
can use statements or switchcase tags in goto statements to specify programs whose branches exceed initializers. This type of code is illegal unless the declaration containing the initializer is in a block that is closed in the block where the jump statement occurs.
The following example shows a loop for declaring and initializing objects total, ch, and i. There are also errors in the goto statement that pass control over the initializer.


// transfers_of_control.cpp
// compile with: /W1
// Read input until a nonnumeric character is entered.
int main()
{
  char MyArray[5] = {'2','2','a','c'};
  int i = 0;
  while( 1 )
  {
   int total = 0;

   char ch = MyArray[i++];

   if ( ch >= '0' && ch <= '9' )
   {
     goto Label1;

     int i = ch - '0';
   Label1:
     total += i;  // C4700: transfers past initialization of i.
   } // i would be destroyed here if goto error were not present
  else
   // Break statement transfers control out of loop,
   // destroying total and ch.
   break;
  }
}

In the previous example, the goto statement attempted to pass control over the initialization of i. However, if i is declared but not initialized, the pass is legal.
Objects declared in chstatement blocks that are used as total statements and while are destroyed when they exit the block using break statements.


Related articles: