C + + Learning Summary of the statement _c language

Source: Internet
Author: User
Tags pack readline try catch

First, sequential statements

Second, the condition, the branch statement

1, if statement

The key is to be able to skillfully use if nesting. Consider all the circumstances.

If the conditions are the corresponding to each other, then you can use only if and else. But you have to think about how each else is paired with which if.

If the situation is independent of the three cases above, then you can choose to use if ... else if ...

1.if statement

if (condition)
{
Meet the requirements of the time to implement;
}

2. if (condition)

{
Meet the requirements of the implementation;
}
Else
{
Execution when conditions are not met;
}

3 if (condition 1)
{
Meet the conditions of 1 when the implementation;
}
else if (condition 2)
{
satisfies the condition 2 without satisfying the condition 1;
}

4.

if (condition 1)
{
if (condition 2)
{
It satisfies the condition 1 and satisfies the condition 2.
}
}

2. Switch statement

If you have a long choice of conditions, choosing a switch statement is more efficient than an if statement. Special attention is the case followed by the break.

eg

eg.6 Swtich Statement Scope
static void Maine (string[] args)
{
Console.WriteLine ("The hero you chose to play for this time is:");
Random r = new Random ();
int n = r.next (10);

String A;

Switch (n)
{
Case 1:
A = "Zhaoxin"; Break
Case 2:
A = "Ice shooter";
Case 3:
A = "The Infinite Sword is holy";
Case 4:
A = "robot"; Break
Default
A = "Qi Tian sheng";
}
Console.WriteLine ("The hero of this choice is:" +a);
}

Third, the circular statement

For loop

Four elements:

Initial conditions, cyclic condition, state change, circulation body. Execution process:

Initial conditions--cyclic condition--cyclic body--state change--cyclic condition ....

Note: For parentheses are separated by semicolons, and for parentheses do not add semicolons.

With a breakpoint, you can better understand how the for works.

1.for Loop NULL operation completed example, output 100 of the number

 static void Main (string[] args)
     {
       int i = 1;
       for (;;)
       {
         if (i >)
         {break
           ;
         }
         Console.Write (i + "t");
         i++;
       }
       Console.readkey ();
     }

Of course, you can also use the while,if () break, nested to complete the above operation.

. The inference problem of positive sequence and reverse order. (Origami problem)

Eg.5 Origami Problem

     static void Maine (string[] args)
     {
       //console.writeline ("Please enter Times");
       int n = Convert.ToInt32 (Console.ReadLine ());
 
 
       int i = 0;
       for (double = 0.0001 sum <= 8848.0; sum = sum * 2)
       //{
       //  i++;
 
       }
       //console.writeline (i);
 
       Double sum = 0.0001;
       int z = 0;
 
       for (int i = 0;; i++)
       {
         z++;
         sum = sum * 2;
 
         if (sum >= 8848.0)
         {
           Console.WriteLine (z);
           break;
         }}}
     

Application: A. Exhaustive method: the use of the cycle of the various possible conditions to go through, and then use if conditions to meet the requirements of the results of screening.

Eg.6 hundred Ma Baishi big horse Pack 2 stone, the horse Pack 1 Stone Pony pack 0.5 stone

    static void main6a (string[] args)
     {for
       (int i = 0; I <= to i++)
       {for
         (int j = 0; J <=; j +) 
   {for
           (int k = 0; k <= k++)
           {
             if (I * 2 + J * 1 + k * 0.5 = = && (i + j + k = 100))
             {
               thread.sleep ();
               Console.WriteLine ("Big horse needs" + i + "head", the horse needs "+ j +" Head, Pony needs "+ K +" head. ");
             }
           }
         }
       }
     }

eg.7

     static void Maing (string[] args)
     {for
       (int i = 1; i < i++) {for
         (int j = 1; j < 5; j +)
         { c6/>for (int k = 1; k < k++)
           {
             if (I * 5 + j * + k = =)
             {
               Console.WriteLine ("50 dollars for buy" + I . ToString () + "a toothbrush," + j.tostring () + "a toothpaste," + k.tostring () + "block soap, just can run out." ");
             }
           }
         }
       }
 
     }

Eg.8 has 1, 2, 5 pieces of money, a sum of 20 dollars, there are several methods of rounding

     static void Mainh (string[] args)
     {
       int m = 0;
       for (int i = 0; I <=. i++)
       {for
         (int j = 0; J <= + j) {for
           (int k = 0; k < 4; k++)
           {
             if (i * 1 + 2 * j + 5 * k = =)
             {
               m++;
               Console.WriteLine ("A total of + M +" in the method.) ");
               Console.WriteLine ("Need 1 Yuan + i +" Zhang, 2 yuan "+ j +" Zhang, 5 yuan "+ k +" Zhang.) ");
             }
           }
         }
       }
     }

eg.9 1 () 2 () 3 () 4 = 4; Ask me to fill in the brackets (-or +)

     static void Maini (string[] args)
     {for
       (int i = 1; I <= 1; i + 2)
       {for
         (int j =-1; J <= 1; j = 2
         {for
           (int k =-1; k <= 1, k = 2)
           {for
             (int l =-1; l <= 1; l = 2)
             {
               if (1 * i + 2 * j + 3 * k + L * 4 = 4)
               {
                 Console.WriteLine ("i=" + i +), j= "+ j +", k= "+ K +", l= "+ L +". ");
               }
             }
 
 
           }
         }
       }
     }

eg.10 123 () 45 () 67 () 8 () 9=100 required to fill in () + or-to make the equation set up.

     static void Maini2 (string[] args)
     {for
       (int a =-1; a <= 2; a + 2)
       {for
         (int b =-1; b <= 2; b = = 2)
         {for
           (int c =-1; C <= 2; C + + 2)
           {for
             (int d =-1; d <= 2; d = 2)
             {
               if (123 + A * 4 5 + b * + c * 8 + d * 9 =
                 Console.WriteLine ("A=" + A + ", b=" + B + ", c=" + C + ", d=" + D);
             }
         }
       }
       Console.readkey ();
     }

eg.11 a reconnaissance team received an urgent mission to select as many people as possible among the six members of the a.b.c,d,e,f. Both A and B must go to one person. A and D cannot go at the same time. A,e,f Three men must go to the two. Both B and C
To go or not to go. C and D two to one person. If D does not go, E will not go. How many people should be called? (Flexible use 1 and 0)

     static void Mainj (string[] args) {for (int a = 0; a <= 1; a++) {for (int b = 0; B & Lt;= 1;
             b++) {for (int c = 0; c <= 1; C + +) {for (int d = 0; d <= 1; d++) 
                 {for (int e = 0; e <= 1; e++) {for (int f = 0; f <= 1; f++) {if ((A + B >= 1) && (A + D <= 1) && (A + e + f = = 2) &&am P (b + C!= 1) && (c + d = = 1) && (d-e >= 0)) {Console.writeli
                   NE ("a=" + A + "b=" + B + "c=" + C + "d=" + D + "e=" + E + "f=" + F);  }}}}}}//teacher version static void
       Mainj1 (string[] args) {int A, B, C, D, E, F; for (a = 0; a < 2, a++) {for (b = 0; b < 2; b++) {for (c =0; C < 2;
               C + +) {for (d = 0; d < 2; d++) {for (e = 0; e < 2; e++)  {for (f = 0; f < 2; f++) {if ((A + B >= 1) &&  (A + D <= 1) && (A + e + f = = 2) && (b + C!= 1) && (c + d = = 1) && ((d + e = = 0) | |  = = 1)) {Console.WriteLine ("a=" + A + "b=" + B + "c=" + C + "d=" + D + "e=" + E +
                   "f=" + F);
     }}}}} Console.readkey ();
 }

B. Iterative method: There are certain laws. Each loop is the result of the last operation to obtain data, the result of this operation is to prepare for the next operation.

The problem of Eg1 Rabbit's birth rabbit

There are a pair of young rabbits, the young rabbit one months after the growth of the rabbit, the rabbit one months later grew into a rabbit and gave birth to a pair of young rabbits, asked how many years after the rabbit, of which the young rabbit, Rabbit, the number of rabbits, respectively?

The problem of eg.2 Rabbit's birth rabbit

    Method one static void Maink3 (string[] args) {int syt = 1, byt = 0;
      int SXT = 0, bxt = 0;

      int SCT = 0, BCT = 0;
      Console.WriteLine ("Please enter the number of months:");
      int month = Convert.ToInt32 (Console.ReadLine ());

      int sum;
        for (int i = 1; I <= month i++) {//Assignment order does not change, must follow the rabbit growth rule, the first some BCT will have byt BCT = SXT + SCT;
        BXT = SYT;

        Byt = SXT + SCT; BCT = SXT + SCT;
        This writing, must pay attention to his order//bxt = SYT;



        Byt = BCT;
        Byt = bct;//wrong//bxt = SYT; 

        BCT = SXT + SCT;
        SYT = byt;
        SXT = BXT;

        SCT = BCT;
      sum = byt + bxt + BCT;

      sum = byt + bxt + BCT; Console.WriteLine ("After {0} months, the number of young rabbits is {1} pairs, the number of rabbits is {2} pairs, the number of rabbits is {3} pairs, a total of {4} pairs." ", month.
     
    ToString (), byt, BXT, bct,sum);
      }//Method two static void Maink4 (string[] args) {int n = Convert.ToInt32 (Console.ReadLine ()); int tu = 0;//requires the total number of that month int tu1=1, tu2=1;//the penultimate first for TU1, and the second to TU2 for (int i = 3; I < n;i++) {Tu = tu1 + tu2;
        TU2 = TU1;
      TU1 = Tu;

    } Console.WriteLine (TU);
      }//Method three static void Maink5 (string[] args) {Console.Write ("Please enter the number of months:"); int m = Int.
      Parse (Console.ReadLine ()); 
      int ct = 0;//log int xt = 0;//rabbit log int yt = 1;//int zt = 1;//for (int i = 1; I <= m; i++)
          {if (i = = 1) {ct = 0;
          XT = 0;
        YT = 1;
          else {ct = xt + ct;
          XT = YT;
        YT = ct;
        
        ZT = yt + xt + ct; The logarithm of Console.WriteLine (i.tostring () + "after a month" is: "+ ct.
        ToString ()); Console.WriteLine (i.tostring () + "after a month the logarithm of the bunny is:" + XT.
        ToString ()); Console.WriteLine (i.tostring () + "The logarithm of the young rabbits after a month is:" + YT.
        ToString ()); Console.WriteLine (i.tostring () + "months after the rabbit's total logarithm is:" + ZT.
      ToString ());
    } console.readline ();

 }

The and of all numbers within the number of eg 2 100.

Eg3. Ask for factorial.
Eg4. Ask for age.
Eg5. Origami.
Eg6. The board puts the grain on.
Eg7. Monkeys eat peaches.

 static void Maink (string[] args)
    {
      int sum = 1;
      for (int i = 0; i < 6; i++)
      {
        int t = (sum + 1) * 2;
        sum = t;
      }
      Console.WriteLine ("Peach" altogether has: "+ sum +"). ");
    }

Eg8. Falling ball problem. A ball from the height of 10 meters, each bounce up 2/3 height, ask the fifth time after the height of the bounce?

four, while loop. generally used in some dead loops.

v. Try catch. protects the program from running without a program error.

Format:

 try//Shortcuts: Double-click the tab                                    
      {

      }
      catch (Exception)
      {

        throw
      }
      Finally
      {
 
      }

The above mentioned is the entire content of this article, I hope you can enjoy.

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.