Today we will share with you the operators and condition structures in C.
In the preface, We have briefly understood the C # language and probably understood the basic content of C, in future analysis, we will Package C # into our brain step by step for future calls.
OK! Let's discuss the operators and condition structures today. First, let's take a look at the simple operators in C #. When it comes to operators, we may think of addition, subtraction, multiplication, division, of course, C # also involves addition, subtraction, multiplication, and division. Of course, there may be some differences between some meanings and primary school mathematics, but some are still the same. Let's take a look at the No. 1 Arithmetic Operator below:
No. 2 value assignment operator "=" can be said to be used in many projects, and the frequency of use is also very high. This value assignment operator means to assign the value on the right of "=" to the left of "=, for example: int qingpingguo = 23; this means that the value 23 on the right is assigned to the qingpingguo on the left.
No. 3 comparison operator:
No. 4 logical operators:
These four operators are commonly used in C #. These are common items in projects. As long as we understand what they mean, we can explain them too much, let's focus on the condition structure!
The condition structure is generally used for selection, such as whether the user saves data in the system, whether the user exits the system, and some judgment statements.
Let's take a look at the simple if structure statement first. Let's look at an example of a console program:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 string choose;
6 Console. WriteLine ("Enter your choice: A: I like qingapple, B: I really like qingapple ");
7 // accept the selected answer
8 choose = Console. ReadLine ();
9 // if to judge and output the result
10 if (choose = "")
11 {
12 Console. WriteLine ("I like qingapple ");
13}
14 else
15 {
16 Console. WriteLine ("I really like qingapple ");
17}
18 Console. ReadKey ();
19}
20}
Running result:
This is just a very simple small example. As long as you understand the principles of this example, you can make a wonderful addition on this basis.
No.1 simple if statement Syntax:
If (conditional expression)
{
Code block;
}
Else
{
Code block B;
}
Flowchart:
Flowchart Description: if (if) The choose value entered by the user is equal to (=) ("A"), the result of the conditional expression is true, and the program executes code block, do not execute code block B; otherwise (else, that is, the choose selected by the user is not equal to the value "A" and the conditional expression result is false), the program executes code block B, the program does not execute code block.
No. 2 Syntax of multiple if statements:
If (conditional expression)
{
Code block;
}
Else if (conditional expression B)
{
Code block B;
}
Else if (conditional expression C)
{
Code Block C;
}
Else
{
Code block D;
}
Flowchart:
Flowchart Description: if the result of condition expression A is true, the program executes code block A. Otherwise, if the result of condition expression A is false, the result of condition expression B is true, program Execution code block B; otherwise, if both conditional expressions A and B are false, condition expression C returns true, and the program executes code block C; otherwise (that is, returns false for all conditional expressions), Program Execution code block D
No. 3 Syntax of nested condition statements:
If (conditional expression)
{
If (conditional expression B)
{
Code block;
}
Else
{
Code block B;
}
}
Else
{
Code Block C;
}
This nested flow chart is similar to the flow chart of a simple if statement, except that the if condition contains a simple if statement, so I won't draw any picture. Let's talk about the principle, in the nested syntax above, if the result of condition expression A is true, continue to judge condition expression B. If the result of condition expression B is true, execute code block, otherwise, the result of conditional expression B is false, and the Execution code block B; otherwise, the result of conditional expression A is false, and the Execution Code Block C, the end
In nested statements, we should note that only when the outer if condition is met can we enter the memory if condition judgment. In addition, the results of conditional expressions can only be bool values (true, false ).
In C #, there are two condition structures. Besides the if structure, there is also a multi-condition structure switch.
Let's take a look at an example of a console program:
1 class Program
2 {
3 static void Main (string [] args)
4 {
5 string choose;
6 Console. WriteLine ("Enter your choice: A: I like qingapple, B: I really like qingapple, C: I like qingapple very much ");
7 // accept the selected answer
8 choose = Console. ReadLine ();
9 // if to judge and output the result
10 switch (choose)
11 {
12 case "":
13 Console. WriteLine ("I like qingapple ");
14 break;
15 case "B ":
16 Console. WriteLine ("I really like qingapple ");
17 break;
18 case "C ":
19 Console. WriteLine ("I like qingapple very much ");
20 break;
21 default:
22 Console. WriteLine ("input error ");
23 break;
24}
25 Console. ReadKey ();
26}
27}
Running result:
Structure syntax of the switch statement:
Switch (value)
{
Case constant value:
Code block;
Break;
Case constant value:
Code block B;
Break;
Default:
Code Block C;
Break;
}
Flowchart:
Flow chart Description: The switch statement has multiple options. We can use a lot of cases for selection. The case here is the keyword. If the constant value following is the same as the value in the switch (value), execute the code block under the case. Break is also a keyword used to interrupt conditional statements. If the break keyword is executed, this switch statement ends. When the constant values after all cases are not equal to the values in the switch (value), run the code below the default keyword.
Note that the values in the switch (value) must be of the int, char, or string type, as long as there are statements in each case block and default, the keyword break is required.
Okay! Finally, let's look at a special ------ conditional operator.
When we make some simple judgments, we feel that it is not necessary to write so complicated. It is just a true or false judgment! Then we can use conditional operators to replace them. It is more concise and easier to grasp than the if structure.
Let's take a look at the syntax of conditional operators:
Variable = conditional expression? Value A: Value B. This expression means that if the result of the conditional expression is true, value A is assigned to the variable. Otherwise, the result of the conditional expression is false, the value B is assigned to the variable.
For example! Write an example to better understand: int qingpingguo = number> 18? 20: 10;
That is to say, if the number is greater than 18, 20 is assigned to qingpingguo. If the number is not greater than 18, 10 is assigned to qingpingguo.
OK! This is a conditional operator, which is easy to use. However, it is recommended that the if structure be used for Complex Condition determination.
Summary: 1. This analysis mainly introduces arithmetic operations, value assignment operators, comparison operators, and logical operators in C.
2. Simple if statements, multiple if statements, and nested if statements in the condition structure.
3. switch statement in the condition structure.
4. Special conditional operators.
Here is the sharing! Or that sentence: This article is my personal opinion. If it is not perfect or accurate, you are welcome to criticize it and give your advice.
From apple