Step by step Follow Yang zhongke. net Video c # BASICS (1)

Source: Internet
Author: User
Tags case statement switch case

2011.2.19 AM

Learn about yangzhongke. net Video c # knowledge points summarized after the basics: (these knowledge points are all the content that Mr. Yang zhongke mentioned in the lecture or on the courseware. I will collect and organize them, I hope to share with you what you want to learn. network friends help)

1. Use C # To compile a 10 + 20 =? Small Program:

Public static void Main (tring [] args)

{

Int i1 = 10;

Int i2 = 20;

Int i3 = i1 + i2;

Console. writeLine (i3); // It can also be implemented using placeholders: Console. writeLine ("{0} + {1} = {2}", i1, i2, i1 + i2); Use placeholders when many output parameters are used.

Console. ReadKey (); // note the use of the ctrl + j shortcut in the MS-VS

}

2. Steps for displaying code row numbers in the MS-VS: Tools> Options> text editor> c #> display> line numbers.

3. i. c # usage of the escape Letter "\": Response. write (@ "c :\... \... \ abc.txt ") or Response. write ("c :\\... \\... \ abc.txt "). @ Only applies to escape characters and cannot solve the problem of double quotation marks output in strings. The latter is troublesome and generally does not need to be used.

Eg: Print "AB" on the screen"

String s = "\" \"";

Console. Write (s );

Ii. escape characters have nothing to do with memory storage:

Eg: string s = Console. ReadLine (); // The content returned by Console. WriteLine () is always of the string type.

Console. WriteLine (s );

After running, enter a \ nb on the screen to print a \ nb, rather than press enter to wrap B.

III: int I = Convert. ToInt32 (Console. ReadLine () // This method is correct, but no container is created to store data.

4. linefeed "\ n": Response. Write ("a \ nb"). // The Escape Character is only applicable to strings directly written in the Code and is invalid for the strings read by the program.

5. variables: variables can be seen as containers for storing data. They cannot be duplicated during definition to ensure data security and accuracy.

(It does not mean that the name of the variable cannot be the same as eg: {int a = 5;} int a = 5; the defined variable can run normally as long as the method or class is different ).

Naming rules: the first letter must be a letter or underline, and the subsequent character can be any number, letter or underline. You cannot use the c # keyword for variable names.

Eg: int int1 = 5; // The write is correct.

// C # keyword determination method: the characters displayed in blue in VS are all c # keywords. The naming rules and functions of variables and classes are the same.

// In c #, you can use Chinese as the variable name | function name | Class Name

6: variable type: int, char, string, bool, double (understanding), byte (understanding), decimal (understanding), long (understanding), float (understanding, in the database, this is generally used for the wage data type ).

7: 'A' is of the char type; ''a' is of the string type. A string can be considered to be composed of multiple characters.

8: type conversion convert: Convert. ToString (), Convert. ToInt32 (), ToString ().

9: Operator expression:

● +,-, *,/(Except), % (remainder)

● + Can be used as a string connection. others cannot.

● ++ Auto-increment; -- auto-Increment

● Operator priority: i1 + i2 * i3 and (i1 + i2) * i3; do not persist, brackets are king

● The variable can be mixed with the word volume: eg: i1 = i2 + 20

10. Assignment: I + = 2; // I = I + 2

(*) + =,-=, * =,/=;

11. int x, y = 10, 0;

X + y = 80; // This write is incorrect, because the left must be a variable !! It cannot be a constant or an expression.

Console. Write (y );

12. Console. Write ("{0}", I = 1); // The value expression in c # also has a value. Its value indicates the value of the variable after the value is assigned.

Console. Write ("{0}", I = 1); and Console. Write ("{0}", I = 1.

13. if statement:

Int age = 20

If (age> 20)

{Console. Write ("adult ")}

Else if (age> 12)

{Console. Write ("children ")}

Else

{Console. Write ("infants")} // if, else can omit {} when there is only one line of code behind the keyword. However, the second line of code after the keyword does not have any relationship with the keyword.

// It is best to add braces when the Gu if statement has only one line of code.

2011.2.19 pm

14. Nesting of if statements and statements:

Eg1: indicates the user's age. If the age is greater than or equal to 18, the user is notified to be able to view the data. If the age is less than 10, the user is notified that the data cannot be viewed. If the age is greater than or equal to 10, if yes is entered, the system prompts you whether to continue viewing (yes, no). If yes is entered, the system prompts you to view the information. Otherwise, the system prompts you not to view the information. (Test boundary condition,-1, 8888888888888888888888888. Microsoft's Tester is like a wolf ). The Code is as follows:

Namespace test
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("Enter your age :");
String strage = Console. ReadLine ();
Int age = Convert. ToInt32 (strage );
If (age> 18)
{
Console. WriteLine ("You can continue viewing! ");
}
Else if (age <10)
{
Console. WriteLine ("sorry, you cannot continue viewing! ");
}
Else
{
Console. WriteLine ("You are under 18 years old. If you reply" yes ", you can continue to view it. If you reply" no ", the system exits! ");
If (Console. ReadLine () = "yes ")
{
Console. WriteLine ("You can continue viewing! ");

}
Else if (Console. ReadLine () = "no ")
{
Console. WriteLine ("Good! ");

}
Else
{
Console. WriteLine ("the error you entered! ");
}
}
Console. ReadKey ();

}
}
}

Eg2: In turn, the user is prompted to enter two integers (i1, i2 ). If i1 and i2 are all positive numbers, increase the value of i1 to one, and then print the value of i1 + i2. If i1 and i2 are all negative, then the value of i1 is reduced to 10, then print the i1 * i2 value. If either of i1 and i2 is 0, an error is returned. Otherwise, the absolute value of i1 * i2 is calculated. The Code is as follows:

Namespace test
{
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("enter an integer :");
String stri1 = Console. ReadLine ();
Int i1 = Convert. ToInt32 (stri1 );
Console. WriteLine ("enter an integer again :");
String stri2 = Console. ReadLine ();
Int i2 = Convert. ToInt32 (stri2 );
If (i1> 0 & i2> 0)
{
I1 = i1 + 1;
Console. WriteLine ("{0} + {1} = {2}", i1, i2, i1 + i2 );
}
Else if (i1 <0 & i2 <0)
{
I1 = i1-10;
Console. WriteLine ("{0} * {1} = {2}", i1, i2, i1 * i2 );
}
Else if (i1 = 0 | i2 = 0)
{
Console. WriteLine ("data error, please input again :");
}
Else
{
Int i3 = i1 * i2;
If (i3 <0)
{
I3 =-i3;
}
Console. WriteLine (i3 );
}
Console. ReadKey ();
}
}
}

15. switch case statement: the determined value is executed in the branch that meets the condition. The value in case must be a constant, not a variable or expression.

● It is similar to if... else... elseif... else, but it is a discrete value.

● The switch can be rewritten with if, but if cannot be rewritten with switch.

● Do not forget the BREAK. It is impossible to write the break in c # without merging the case.

Eg: the code is as follows: (PS: the example given by instructor Yang is humorous. This kind of spirit is also a place I admire, entertaining and entertaining !)

Int I = 2; // modify the I value to test the function of the switch... case... statement.
Switch (I)
{
Case 2: // equivalent to if (I = 2)
Console. WriteLine ("You are really 2! ");
Console. WriteLine ("You only have 2! ");
Break; // The break must be written in c #. Each branch of the switch statement must have a break. Although it can be omitted in C, it may cause a BUG.
Case 4:
Console. WriteLine ("dead! ");
Break;
Case 8:
Console. WriteLine ("sending! ");
Break;
Default: // equivalent to the else of the if statement
Console. WriteLine ("the {0} You entered is meaningless! ", I );
Break;
}
Console. ReadKey ();
● When several logical processing units process the same content, they can combine the content of the logical processing unit.

Eg: int I = 2;

Switch (I)
{
Case 2: // only constants such as "2" and "aaa" can be followed by variables.

Console. WriteLine ("You are really 2! ");
Console. WriteLine ("You only have 2! ");
Break;

Case 8:
Console. WriteLine ("sending! ");
Break;
Case 10:
Case 20: // equivalent to if (I = 10 | I = 20). Here is the only case that does not use break.
Console. WriteLine ("You entered the whole money! ");
Console. WriteLine ("grandpa! You really have money! ");
Break;
Default:

Console. WriteLine ("the {0} You entered is meaningless! ", I );
Break;
}
Console. ReadKey ();

16. while Loop

● While (I <100)

{

// Print I

I ++; // as long as the expression in the parentheses after the while is true, the code in the braces is executed repeatedly, which is like an endless loop.

}

● (*) Do // do first... If this parameter is set to true, run the command again until the while expression is false. The code in parentheses is executed at least once.

{// A rarely used syntax

// Print I

I ++;

}

While (I <100)

Eg1: use while to calculate the integer sum between 1 and 100 (in fact, there is a better solution)

Eg2: requires the user to enter the user name and password. If it is not admin, 888888 will always prompt you to re-enter the password.

Eg3: the user is constantly required to enter a number, and then double the number. When the user inputs q, the program exits.

Eg4: the user is constantly asked to enter a number. When the user enters the end, the maximum value of the number just entered is displayed.

Not yet to be continued .... I will write the article multiple times

Next article:

Http://www.cnblogs.com/CarreyWu/archive/2011/02/20/base_of_Csharp_02.html

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.