C # basic series of Question 1: break, continue, return, and goto statements

Source: Internet
Author: User

I. Break
   1. break statement format: break;
   2. break statement function:
      A. In A switch statement, break is part of its syntax in this province. The break statement terminates the execution of the subsequent statement and exits the switch statement.
      B. terminate a loop immediately. That is to say, when a break statement is encountered in the loop, the loop is terminated immediately. The program proceeds to the first statement after the current loop body.
   3. Note:
      A. When A break statement is used in A loop, it is generally used together with if. When the condition is met (or not met), it is responsible for exiting the loop.
      B. If the switch statement is used in the loop body and the break appears in the switch statement, it is only used to end the switch without affecting the loop.
      C. The break statement can only end the innermost loop containing it, but cannot skip multiple loops.
   4. for example, the break statement can only exit from the while loop and continue to execute other statements of the for loop.
Instead of exiting the outer loop.

For ()
{
...
While ()
{
If ()
{
Break;
}
}
...
}
II. Continue 
   1. Continue Statement Form: continue;
   2. Continue statement function: It can only appear in the loop body. Its function is to immediately end this loop, that is, when a continue statement is encountered, the statement after the continue in the loop body is not executed, immediately go to determine whether the cycle condition is set, that is, stop the current cycle and enter the next cycle.
   3. the difference between a continue and a break statement: A continue only ends this loop, rather than terminating the execution of the entire loop statement. A break terminates the execution of the entire loop statement, go to the next statement after the current loop statement.
    The program expression and flowchart are as follows:
(1 .)While (expression 1)                (2.) while (expression 1)
{                                  {
     ......                           ......

          If (expression 2) break;               If (expression 2) continue;
          ......                                ......

      }                                  }

Iii. goto
   1. GOTO statement format: The GOTO statement is an unconditional turning statement. The general format is the GOTO statement label;
   2. function: The GOTO statement is often used to jump out of multiple loops. It is convenient to solve some specific problems, but the GOTO statement is difficult to control and should be used as little as possible.
   3. Example:

When the program executes a goto statement, it jumps to the statement label pointed to by the goto statement to start execution.
          IN:
             For ()
             {
                  ......
                  Goto in;
              }
            

Iv. Return 
   1. return Statement Form: return (return value );
   2. function: return returns the value of a function and jumps out to end the function;
If you encounter a return statement, the program stops executing the code in that line. The execution control will immediately return to the code that calls the program.
For programs whose return value type is void, the return keyword is used separately as the complete statement: return;
Instance:

1) when return is a string type, convert a string to lowercase.

// Converts a string to lowercase and returns the result. The function is the same as that of string. ToLower.
Public static string ToLower (string str)
{
Char [] array = new char [str. Length]; // defines an array of characters. The Length is equal to the Length of the given string.
Int diff = 'a'-'A'; // calculate the difference value. The ASCII code value is a <A (fixed. You don't need to know the difference value)
// Use a loop to read a given string and read one character at a time
For (int I = 0; I <str. Length; I ++)
{
Char ch = str [I]; // assign values to the characters read by the current loop
Char newch = '\ 0 ';
// Determine whether the character is in uppercase.
If (char. IsUpper (ch ))
{
Newch = (char) (ch + diff); // convert to lowercase
}
// If the character is lowercase, It is not converted.
Else
{
Newch = ch;
}
Array [I] = newch; // place the converted character back to the position of this loop string
}
Return new string (array );
}

2) When returning an array: return an array of int type as an example.

Note: When returning an array, you must declare an array of the same type when using this method to accept the array returned by the method.

Public class contains
{
// Output in sequence is smaller than all numbers with 7 and multiples of 7 for the given number
Public static int [] contains7 (INT number)
{
Int n = 0; // records the number of results that meet the requirements.
For (INT I = 1; I <= number; I ++)
{
// Determine whether it is a multiple of 7
If (I % 7 = 0)
{
N ++;
Continue;
}
String str = I. ToString ();
If (str. Contains ('7 '))
{
N ++;
}
}
Int [] arry = new int [n]; // declare an integer array to store the result. The length is exactly the same as the required number.
Int index = 0;
// List <int> arry = new List <int> ();
For (int j = 1; j <= number; j ++)
{
// Determine whether it is a multiple of 7
If (j % 7 = 0)
{
Arry [index ++] = j;
Continue;
}
String str = j. ToString ();
If (str. Contains ('7 '))
{
Arry [index ++] = j;
}
}
Return arry;
}
Public static void Main (string [] args)
{
Console. writeline ("enter a number :");
Int COUNT = convert. toint32 (console. Readline ());
// Declare an integer array for receiving (static class)
Int [] arry = contains. contains7 (count );
// Cyclically output results
For (INT I = 0; I <Arry. length; I ++)
{
Console. Write ("{0} \ 0", arry [I]);
}
Console. Read ();
}
}

I hope people with the same interests can join the group 128874886, learn together, share and make progress together!

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.