If else, array of small exercises.

Source: Internet
Author: User

logical operators

&&: Two operator, true on both sides. The result is true.

|| : A two-dollar operator, as long as one is true and the result is true.

!: unary operator, reverse, true false, false change true.

Short circuit operation

The difference between & and && two. (& all comparison values will be calculated) use && sometimes short-circuiting.

|| Because as long as one condition is true, no subsequent operations are performed.

Ternary operators

int Ten  int); // If I>100 then J is 50. The reverse is
can simplify the number of code. strings = gender?"male":"female"when gender is true, gender output male, otherwise female. //The practice input value, whether it can be divisible, if it is an even number, cannot be odd, use the ternary operator to calculate. stringi =Console.ReadLine (); stringAnswer = ((Convert.ToInt32 (i)%2==0) ?"This number is even":"This number is odd");            Console.WriteLine (answer); Console.readkey ();

Standard notation for if else

int i=ten; if (i>) {Console.WriteLine (" Please enter ");} Else {Console.WriteLine ("i<18 shall not enter. ");}

Gender Judgment Exercise

BOOLBoy =radiokboy.checked; BOOLGirl =radiogirl.checked; if((boy| | Girl) = =false) {MessageBox.Show ("Please select gender;"); return; }            if(Boy) {MessageBox.Show ("Hello, handsome."); //. WriteLine ("");            }            Else{MessageBox.Show ("Hello, beautiful."); }

Judging gender and age output content

string text = TextBox1.Text;             bool boy = radiobutton1.checked;             bool girl = radiobutton2.checked;             if (text!="")            {                int age = Convert.ToInt32 (text);                 if 0 1 2 3 ))                {
                  if(Girl) {MessageBox.Show ("baby girl"); }                    Else if(Boy) {MessageBox.Show ("baby boy"); }               }                Else if(Age >=4&& Age <= -)               {                   if(Girl) {MessageBox.Show ("Lori, Lisa ."); }                    Else if(Boy) {MessageBox.Show ("is too"); }                }                Else if(Age >= +&& Age <= -)                {                    if(Girl) {MessageBox.Show ("Sister paper"); }                    Else if(Boy) {MessageBox.Show ("Aubagne"); }                }                Else if(Age >= in&& Age <= -)                {                    if(Girl) {MessageBox.Show ("Mature, amateur"); }                    Else if(Boy) {MessageBox.Show ("Sorghum"); }                }                Else if(Age >=Wuyi&& Age <= Max)                {                    if(Girl) {MessageBox.Show ("Granny"); }                    Else if(Boy) {MessageBox.Show ("\ "Ye Ye""); }                }                Else{MessageBox.Show (".. Are you sure you have such a long life? "); }            }            Else{MessageBox.Show ("text box cannot be empty"); }                                

Body mass Index calculation

varText1 =txt1.            Text; varText2 =txt2.            Text; varHeight =convert.todouble (TEXT2); varkg =convert.todouble (Text1); varBMI = kg/(height*height); if(BMI <18.5) {MessageBox.Show ("your index is"+ BMI +"\ n belongs to Skinny ."); }            Else if(BMI >=18.5&& BMI < -) {MessageBox.Show ("your index is"+ BMI +"\ n belongs to normal ."); }            Else if(BMI >= -&& BMI < -) {MessageBox.Show ("your index is"+ BMI +"\ nyou are overweight ."); }            Else if(BMI >= -&& BMI < *) {MessageBox.Show ("your index is"+ BMI +"\ n is obese ."); }            Else if(BMI >= *&& BMI < +) {MessageBox.Show ("your index is"+ BMI +"It's a severe obesity ."); }            Else if(BMI >= +) {MessageBox.Show ("your index is"+ BMI +"It's a very heavy fat ."); }

For loop additive 1 to N

string n = console.readline ();

int num = convert.toint32 (n);

int sum=0; for (int i = 0; i <= num; i++)

{sum = sum + i;

// Long sum = num* (num + 1)/2;

Console.WriteLine (sum);

}

The main differences between Do...while and while

Do...while execution statements are executed at least once,

The EXECUTE statement for while may not execute at one time.

Array

The first type of notation:int[] a1=New int[5];a1[0]=3; a1[1]=5; a1[2]=6; a1[3]=3; a1[4]= -; the second way of writing:int[] A2 = {5,6,5,8,7,9, the, A, One};
array of small exercisesint[] num = { -, About,1,898,211, -,2323}; varsum =0; //Calculation Formula Sum=sum+i//gets the number of num arrays in total            for(inti =0; I < Num. Length; i++) {sum= Sum +Num[i]; Console.WriteLine ("the value of the array is"+num[i]+"the value of sum is"+sum); }            intMin = num[0];  for(vari =0; I < Num. Length; i++)            {                vartemp =Num[i]; if(Temp <=min) {min=temp; }} Console.WriteLine ("so the minimum value is:"+min); Console.readkey ();

If else, array of small exercises.

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.