SoFunction
Updated on 2025-04-06

Introduction to C# Branches and Loops

1. Sequence structure: --> The so-called Sequence structure As the name suggests, it is executed in order. In C#, all code is executed from top to bottom.

2. Branch structure: --> Tripartite expression? :, if...else... , switch statement.   is the so-called conditional statement.

3. Loop structure: --> while, do...while... ,foreach statement. The so-called loop statement

  

Conditional statement:

if statement

Translated: If (...) then (...)

else if pairing rules: else is always paired with its closest if, unless other options are indicated by a flower cross sign.

Copy the codeThe code is as follows:

            int a = 7;
            if (a > 0)
            {
                ("this is dog");
            }
            else
            {
                ("this is cat");
            }

//or

            if (a > 0)
            {
                //...
            }
            else if (a == 0)
            {
                //...
            }
            else
            {
                //...
            }

switch statement.

int a= 4;  When the value of a is equal to the value after case, execute the case statement. If neither matches, execute default

Copy the codeThe code is as follows:

          switch (a)
            {
                case 1:
                    return 1;
                    break;
                case 2:
                    return 2;
                    break;
                default:
                    return 3;
                    break;
            }

There is a goto statement in the conditional statement, which is not commonly used, so I will talk about it.

Jump to the specified mark line: The following code When I enter a , the program will go. After entering the third case, goto case "Mark2" will jump to the second case statement to execute the code inside.

Copy the codeThe code is as follows:

static void Main(string[] args)
         {
             string mark = ();
             int cons = 20;
             switch (mark)
             {
                 case "Mark1":
                     goto Mark1;
                 case "Mark2":
                     cons += 20;
                     break;
                 case "a":
                     goto case "Mark2";
                 default:
("The node you entered is not found");
                     return;
             }
             Mark1:
//("Skip to Mark1");
             //return;

             (cons);

           }

Loop statement:
while,do...while... ,foreach statement

While(condition){} , while is very similar to if statements. Both code execution is faster if the condition is met. The difference between the two is that while the code body will be repeatedly executed when the condition is established, while the if statement is only executed once.

do{statement that requires loop execution}while (condition); basically the same as while. Just do while will execute the loop body to determine the condition.

for loop, for loop can be said to be the most commonly used loop in C#.

for(condition initialization; loop conditions; condition change)

Let's do an operation that accumulates to 100

Copy the codeThe code is as follows:

          int sum = 0;
            for (int i = 0; i <= 100; i++)
            {
                sum += i;
            }

foreach loop statement: is a loop statement that automatically traverses. There is a string array as follows, we need to iterate over it. First, every bit in the array is of type string

So we first define a string s and this variable s belongs to the arr array, so the in arr  program will automatically complete the loop. in the previous variable type in foreach

It must be consistent with the type of the array or anything you are traversing, otherwise an error will be reported.

Copy the codeThe code is as follows:

          string[] arrt = new string[] {"a","b","c" };
            foreach (string s in arrt)
            {
                (s);
            }

Conditions and loops are relatively simple. . . .

Two keywords involved are continued break

Then tell me the difference between them.

The difference between break in loop statement is that break exits the loop

When the loop statement is executed to break, this loop statement will pop up. And continue  just ends the current loop and enters the next loop.

Let’s take a look at the following two loop statements; the first one outputs 0 1 2 and the second one just skips 3.

Copy the codeThe code is as follows:

        for (int a = 0; a <= 100; a++)
            {
                if (a == 3)
                    break;
                (a);
            }

            for (int a = 0; a <= 100; a++)
            {
                if (a == 3)
                    continue;
                (a);
            }