Detailed C++ programming markup statements and compound statements

  • 2020-05-07 20:10:02
  • OfStack

markup statement
The
tag is used to transfer program control directly to a specific statement.


identifier : statement
case constant-expression : statement
default : statement

The scope of the label is the entire function, where the label has been declared.
note
There are three kinds of markup statements. They all use colons to separate certain labels from statements. The case and default tags are specific to the case statement.

#include <iostream> 
using namespace std; 
void test_label(int x) {

  if (x == 1){
    goto label1;
  }
  goto label2;

label1:
  cout << "in label1" << endl;
  return;

label2:
  cout << "in label2" << endl;
  return;
}

int main() {
  test_label(1); // in label1 
  test_label(2); // in label2
}

goto statement
The identifier tag appearance in the source program declares one tag. Only goto statements can transfer control to identifier tags. The following code snippet illustrates the use of goto statements and identifier tags:
Tags cannot appear independently and must always be attached to statements. If the label needs to appear independently, you must place one null statement after the label.
A label has a function scope and cannot be redeclared in a function. However, the same name can be used as a label in different functions.


// labels_with_goto.cpp
// compile with: /EHsc
#include <iostream>
int main() {
  using namespace std;
  goto Test2;

  cout << "testing" << endl;

  Test2:
   cerr << "At Test2 label." << endl;
}

//Output: At Test2 label.

case statement
Tags displayed after the case keyword cannot be displayed outside the switch statement. (this restriction also applies to the default keyword.) The following code snippet demonstrates the correct use of the case tag:


// Sample Microsoft Windows message processing loop.
switch( msg )
{
  case WM_TIMER:  // Process timer event.
   SetClassWord( hWnd, GCW_HICON, ahIcon[nIcon++] );
   ShowWindow( hWnd, SW_SHOWNA );
   nIcon %= 14;
   Yield();
   break;

  case WM_PAINT:
   memset( &ps, 0x00, sizeof(PAINTSTRUCT) );
   hDC = BeginPaint( hWnd, &ps ); 
   EndPaint( hWnd, &ps );
   break;

  default:
   // This choice is taken for all messages not specifically
   // covered by a case statement.

   return DefWindowProc( hWnd, Message, wParam, lParam );
   break;
}

Tags in case statements
Tags displayed after the case keyword cannot be displayed outside of the switch statement. (this restriction also applies to the default keyword.) The following code snippet demonstrates the correct use of the case tag:


// Sample Microsoft Windows message processing loop.
switch( msg )
{
  case WM_TIMER:  // Process timer event.
   SetClassWord( hWnd, GCW_HICON, ahIcon[nIcon++] );
   ShowWindow( hWnd, SW_SHOWNA );
   nIcon %= 14;
   Yield();
   break;

  case WM_PAINT:
   // Obtain a handle to the device context.
   // BeginPaint will send WM_ERASEBKGND if appropriate.

   memset( &ps, 0x00, sizeof(PAINTSTRUCT) );
   hDC = BeginPaint( hWnd, &ps );

   // Inform Windows that painting is complete.

   EndPaint( hWnd, &ps );
   break;

  case WM_CLOSE:
   // Close this window and all child windows.

   KillTimer( hWnd, TIMER1 );
   DestroyWindow( hWnd );
   if ( hWnd == hWndMain )
     PostQuitMessage( 0 ); // Quit the application.
   break;

  default:
   // This choice is taken for all messages not specifically
   // covered by a case statement.

   return DefWindowProc( hWnd, Message, wParam, lParam );
   break;
}

Labels in goto statements
The identifier tag appearance in the source program declares one tag. Only the goto statement can transfer control to the identifier label. The following code snippet illustrates the use of goto statements and identifier tags:
Tags cannot appear independently and must always be attached to statements. If the label needs to appear independently, you must place one null statement after the label.
A label has a function scope and cannot be redeclared in a function. However, the same name can be used as a label in different functions.


// labels_with_goto.cpp
// compile with: /EHsc
#include <iostream>
int main() {
  using namespace std;
  goto Test2;

  cout << "testing" << endl;

  Test2:
   cerr << "At Test2 label." << endl;
// At Test2 label.
}


compound statement (block)
The
compound statement contains zero or more statements enclosed in braces ({}). You can use a compound statement at any location where you expect the statement to appear. Compound statements are often referred to as "blocks."
grammar


{ [ statement-list ] }

note
The following example USES a compound statement as part of the statement part of the if statement (see the if statement for more syntax information) :


if( Amount > 100 )
{
  cout << "Amount was too large to handle\n";
  Alert();
}
else
  Balance -= Amount;

Pay attention to
Since the declaration is one statement, the declaration can be a statement within statement-list. Therefore, a name declared in a compound statement (rather than an explicitly static name) has a local scope and (in the case of an object) lifetime.


Related articles: