This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

C # judgment ⛄ ️

The judgment structure requires the programmer to specify one or more conditions to evaluate or test, as well as statements to execute if the condition is true (required) and if the condition is false (optional).

Here is the general form of a typical judgment construct in most programming languages:


statement

C# provides the following types of judgment statements.

statements describe
If statement An if statement consists of a Boolean expression followed by one or more statements.
if… Else statements An if statement can be followed by an optional else statement, which is executed when the Boolean expression is false.
Nested if statements You can use an if or else if statement within another if or else if statement.
A switch statement A switch statement allows testing when a variable is equal to multiple values.
Nested switch statements You can use a switch statement within another switch statement.

? : operator

We have covered conditional operators in the previous section. Can be used instead of if… The else statement. Its general form is as follows:

Exp1 ? Exp2 : Exp3;
Copy the code

Exp1, Exp2, and Exp3 are expressions. Note the use and placement of the colon. ? The value of the expression is determined by Exp1. If Exp1 is true, then the value of Exp2 is calculated and the result is the whole? Value of an expression. If Exp1 is false, the value of Exp3 is calculated and the result is the whole? Value of an expression.


C # cycle 🔔

Sometimes, you may need to execute the same piece of code more than once. In general, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on. Programming languages provide multiple control structures that allow for more complex execution paths. Loop statements allow us to execute a statement or group of statements multiple times. Here is the general form of loop statements in most programming languages:


Circulation type

C# provides the following types of loops.

Circulation type describe
The while loop The statement or statement group is repeated when the given condition is true. It tests the condition before executing the body of the loop.
For/foreach loop Execute a sequence of statements multiple times to simplify the code for managing loop variables.
do… The while loop It is similar to the while statement, except that it tests the condition at the end of the body of the loop.
Nested loop Can be in while, for, or do.. Use one or more loops within a while loop.

Loop control statement

Loop control statements change the normal sequence of execution. When execution leaves a scope, all automatic objects created in that scope are destroyed. C# provides the following control statements.

Control statements describe
Break statement Terminate the loop or switch statement, and the flow continues to execute the statement immediately following the loop or switch statement.
The continue statement Causes the loop to skip the rest of the body, restarting the test condition immediately.

An infinite loop

If the condition is never false, the loop becomes infinite. A for loop can be used to implement an infinite loop in the traditional sense. Since none of the three expressions that make up the loop is required, you can leave some conditional expressions blank to form an infinite loop.

The instanceusing System;

namespace Loops
{
   
    class Program
    {
        static void Main(string[] args)
        {
            for(; ;) { Console.WriteLine("Hey! I am Trapped"); }}}}Copy the code

A conditional expression is assumed to be true when it does not exist. You can also set an initial value and increment expression, but generally programmers prefer to use for(;;). Structure to represent an infinite loop.

A few simple homework 🎅

After reading the knowledge point, do a simple quiz ah (don’t look at the answer first oh, think about your ideas in your head)

1. Enter a three-digit positive integer from the keyboard and output the digits in reverse order

2. Enter any two numbers and calculate their sum and difference product

3. Swap the values of two int variables


Refer to the answerIt’s all very simple, right?

1. Enter a 3-digit positive integer from the keyboard and output method 1 in reverse order

            int b;
            b = Convert.ToInt32(Console.ReadLine());
            int x;
            int y;
            int z;
            x = b % 10;
            y = b /10  %10;
            z = b / 100;
            Console.WriteLine(100*x+10*y+z);
Copy the code

Method 2

 string z = "";
Console.WriteLine("Enter a positive integer");
string str = Console.ReadLine();
for (int i = 0; i < str.Length; i++)
 {
 z = z + str[str.Length - i - 1];    // Use the length of the string -i, and subtract 1 because it starts at 0
// Console.WriteLine(count); //Console.WriteLine(str[str.Length-i-1]); }
Console.WriteLine(z);
Copy the code

2. Enter any two numbers and calculate their sum and difference product

int a, b,c,d,e,f;
            Console.WriteLine("Please enter two numbers:");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            c = a + b;
            d = a - b;
            e = a * b;
            f = a / b;
            Console.WriteLine("The sum of these two numbers is + c + "\n The difference between these two numbers is + d + "\n The product of these two numbers is + e + "\n The quotient of these two numbers is + f);
            Console.ReadKey();
Copy the code

3. Swap the values of two int variables

            int j, k;
            Console.WriteLine("Please enter two numbers j and k.");
            j = Convert.ToInt32(Console.ReadLine());
            k = Convert.ToInt32(Console.ReadLine());
            j = j + k;
            k = j - k;
            j = j - k;
            Console.WriteLine("The value of j is"+j+"The value of \nk is"+k);
Copy the code