Return and GOTO statements for jumps in C + + Process Control Learning Tutorials _c language

Source: Internet
Author: User
Tags function definition goto types of functions

Return statement
terminates the execution of a function and returns control of the calling function (or control of the operating system if you transfer control from the main function). Resumes execution immediately after the call in the calling function.
Grammar

return [expression];

Note
The expression clause (if present) is converted to the type specified in the function declaration, just as it is performing initialization. The conversion of a function from this type of expression to the return type creates a temporary object.

The value of the expression clause returns the Calling function. If this expression is omitted, the return value of the function is indeterminate. Constructors and destructors, as well as functions of type void, cannot specify an expression in a return statement. All other types of functions must specify an expression in the return statement.
When the control flow exits the block of the enclosing function definition, the result is the same as the result of executing a 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 in conjunction with a return statement to get the largest of two integers.

Return_statement2.cpp
#include <stdio.h>

int max (int a, int b)
{return
  (a > B a:b); c8/>}

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 tag statement specified by identifier must be in the current function. All identifier names are members of the internal namespace, so they do not interfere with other identifiers.
The statement label is only meaningful to the Goto statement, and in other cases the statement label is ignored. The label cannot be declared again.
It is a good programming style to use break, continue, and return statements as much as possible rather than goto statements. 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 equals 3 o'clock, the Goto statement transfers control to the point marked as stop.

Goto_statement.cpp
#include <stdio.h>
int main ()
{
  int i, J;

  for (i = 0; i < 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:

An external loop is being executed. i = 0 an
 internal loop is being performed. j = 0
 is performing an internal loop. j = 1
is performing an external loop. i = 1 an
 internal loop is being performed. j = 0
 is performing an internal loop. j = 1
is performing an external loop. i = 2 an
 internal loop is being performed. j = 0
 is performing an internal loop. j = 1
is performing an external loop. i = 3 an
 internal loop is being performed. j = 0
jump to stop. i = 3

Control of the transfer
You can use a statement or switchcase tag in a goto statement to specify a program in which a branch exceeds an initializer. This type of code is illegal unless the declaration containing the initializer is in a block enclosed by the block in which the jump statement occurs.
The following example shows a loop that declares and initializes the total, ch, and I of objects. There is also an error Goto statement that passes control over an 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-I.
   } I would be destroyed this if Goto error were not present
  else
   //break statement transfers control out of loo P,
   //destroying Total and ch.
   break;
  }
}

In the preceding example, the Goto statement attempts to pass control over the initialization of I. However, if I is declared but not initialized, then the pass is legal.
The object and the while that are declared in the block of the chstatement used as the total statement are destroyed when the break statement is used to exit the block.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.