Nested loop statement
Nested loop statement means that the loop body of a loop structure contains another complete loop structure. Embedded loops can also be nested, which constitutes a multi-layer nested loop.
- Nested for loop statements in C #
- Nested while loop statements in C #
- Nested do... while loop statements in C #
using System; namespace Project12 { class Program { static void Main(string[] args) { Console.WriteLine("Please enter the number of lines to print:"); int n = Convert.ToInt32(Console.ReadLine()); //This layer of loop controls the number of lines printed for (int x = 1; x <= n; x++) { //This loop controls the number of spaces printed before each line for (int y = 1; y <= n - x; y++) { Console.Write(" "); } //This layer of loop controls the number of * printed in front of each line for (int z = 1; z <= 2 * x - 1; z++) { Console.Write("*"); } //Change one line after each line is printed Console.Write("\n"); } Console.ReadKey(); } } }
[program analysis] this example demonstrates the use of nested for loops. In the code, double for loops are used. The outer loop controls the number of lines through the variable x. the inner loop has two for loops. The first for loop controls the number of spaces printed in front of each line through the variable y, and there will be one less space for each lower line. Therefore, the relationship with the line is represented by y=n-x; the second for loop controls the number of lines through the variable Z Control the number of asterisks. The number of asterisks in each row is different. The relationship with the row can be expressed by the formula z=2*x-1.
Please enter the number of lines to print: 5 * *** ***** ******* *********
using System; namespace Project13 { class Program { static void Main(string[] args) { int x = 0; do { Console.WriteLine("Enter an integer:"); x = int.Parse(Console.ReadLine()); long y = 1; for (int i = 1; i <= x; i++) { y *= i; } Console.WriteLine(y); } while (x >= 0); Console.ReadKey(); } } }
[program analysis] this example demonstrates the use of circular nested statements. In the code, variable x receives the value entered by the user, and variable y calculates the factorial of the value entered by the user through the nested for loop until a negative number is entered.
Enter an integer: 5 120 Enter an integer: 9 362880 Enter an integer: -4 1
Jump statement
break jump statement
The break statement in C # can be used in the following two ways:
(1) When the break statement is used in loop statements such as do... While, for and while, the program can terminate the loop and execute the statements after the loop structure. Usually, break statements are always associated with if statements, that is, they jump out of the loop when the conditions are met.
(2) It can be used to terminate a case in a switch statement.
continue jump statement
The continue statement in C # is a bit like a break statement. But it is not forced to terminate. Continue will skip the code in the current loop and force to start the next loop.
For a for loop, the continue statement changes the execution condition test and the incremental portion of the loop. For while and do... While loops, the continue statement causes program control to return to conditional testing. Usually, the continue statement is always associated with the if statement to speed up the loop.
using System; namespace Project15 { class Program { static void Main(string[] args) { Console.WriteLine("10 Numbers between 3 and 30 that cannot be divided by 3 and 7 at the same time:"); int i, n = 0; /*n count*/ for (i = 10; i <= 30; i++) { if (i % 3 == 0 && i % 7 == 0) /*If you can divide 3 and 7 at the same time, do not print*/ { continue; /*End the statements not executed in this cycle and continue to judge next time*/ } Console.Write("{0} ", i); n++; if (n % 10 == 0) /*10 Number output one line*/ Console.WriteLine('\n'); } Console.ReadKey(); } } }
[program analysis] this example demonstrates the use of the continue statement. First, two variables i and N are defined in the code. The variable i is used as the loop variable, and the variable n is used for counting. Only when the value of i can be divided by 3 and 7 at the same time, the continue statement is executed. After execution, skip the following statement, directly judge the loop condition i < = 30, and then proceed to the next loop.
10 Numbers between 3 and 30 that cannot be divided by 3 and 7 at the same time: 10 11 12 13 14 15 16 17 18 19 20 22 23 24 25 26 27 28 29 30
return jump statement
The return statement is used to specify the value returned by the function. The return statement can only appear in the function body. It will cause a syntax error anywhere else in the code.
The return statement can be followed by constants, variables, expressions, methods, or nothing. Multiple returns can also appear in a method, but only one will be executed. When nothing is added after the return statement, the return type is void.
goto Statement
Goto statement means jump to. Its function is that when the program executes goto statement, the program will directly jump to the program location identified by the identifier and continue to execute the code.