The iterative method of C # Jump-statement method for poor lifting

Source: Internet
Author: User

First, jump statement break & Continue

Break: Jumps out of the loop, terminates the loop, regardless of how many times there are, skip all.

string a="", for (int i=1; i<=; i++  {        if(i==5)        {                break;        }
     A + = i + ",";
} Console.WriteLine (a);

Output is 1,2,3,4,5

Continue: Stop this cycle and start the next cycle directly.

string a="", for (int i=1; i<=; i++      {        if(i==5)        {                continue;        } A + = i + ",";}    Console.WriteLine (a);

Output is 1,2,3,4,6,7,8

Second, iterative method

Iterative algorithm is a basic method to solve the problem by computer. It uses the computer speed, suitable for repetitive operation characteristics, so that the computer to a set of instructions (or a certain number of steps) repeated execution, each time the set of instructions (or these steps), the variable from the original value of the introduction of its new value.

Iterations in the program:
int0; for (int1; i<=; i++) { a++; }

Iii. the method of poor lifting

The basic idea of the exhaustive method is to determine the approximate scope of the answer based on some of the conditions of the topic, and to validate all possible cases within this range until all the circumstances have been verified. If a situation verifies that all the conditions of the subject are met, then it is a solution to the problem and if all the conditions are not met, then there is no solution. The exhaustive method is also known as the enumeration method.

Representative title: Hundred Chicken Hundred money: There are 100 text money, chicken 0.5 article, Rooster 1 Wen Qian, hen 2 Wen Qian. Ask 100 money to buy chicken, each chicken at least one, all chickens a total of 100, ask how many kinds of possible and list each possibility.

int cont = 0;
for (int1; i<=; i++)
{
for (int GJ = 1;gj<=100;gj++)
{
for (int mj = 1;mj< = mj++)
{
if ((xj*0.5) + (gj*1) + (mj*2) = = && XJ+GJ+MJ = = 100)
{
Console.WriteLine ("Chicken" +xj+ "only, a total of" +xj*0.5 "yuan;" Rooster "+gj+" only, a total of "+gj+" yuan; Hen "+mj+" Total "+mj*2+" only, "Yuan";
count++;
}
}
}
}
Console.WriteLine ("A total of" +count+ "possibilities.) ");

The iterative method of C # Jump-statement method for poor lifting

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.