C # basic parsing-ⅱ [operator and condition structure]

Source: Internet
Author: User

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, first look at a consoleProgramExample:

 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 = " A " )
11 {
12 Console. writeline ( " I like green apple " );
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)
{
CodeBlock;
}
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 green apple, B: I really like green apple, C: I like green apple 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 " A " :
13 Console. writeline ( " I like green apple " );
14 Break ;
15 Case " B " :
16 Console. writeline ( " I really like qingapple " );
17 Break ;
18 Case " C " :
19 Console. writeline ( " I like apple 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)
{
CaseConstant Value:
Code block;
Break;
CaseConstant 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, if the result of the conditional expression is false, 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 there are any imperfections or inaccuracies, you are welcome to criticize them.

author: qingapple
motto: constantly reflecting on yourself! Then change it!
technologies of interest :. net, database, JavaScript, C #, Ajax, winform, jquery, extjs
the source of this article: http://www.cnblogs.com/xinchun/
the copyright of this article belongs to the author and blog park a total, welcome to reprint, however, this statement must be retained without the author's consent and the original article connection is clearly displayed on the Article page. Otherwise, the legal liability shall be held accountable.

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.