The For loop has two classes:
Poor lift:
Go through all the possible situations and use the IF condition to filter out the conditions that satisfy the condition.
Iteration:
From the initial situation according to the law to constantly solve the intermediate situation, the final derivation of the results.
Case:
Unit to send a 150-dollar shopping card,
Take the supermarket to buy three kinds of cleaning supplies.
Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan.
Just spend 150 yuan, how many kinds of buying method,
How do you buy every kind of buying method?
Set Shampoo x 150/15==10
Toothbrush y 150/5==30
Soap Z 150/2==75
int sum = 0;
int Biao = 0;
for (int x = 0; x <= × x + +)
{
for (int y = 0; y <=; y++)
{
for (int z = 0; z <=; z++)
{
sum++;
if (x + 5 * y + 2 * z = = 150)
{
biao++;
Console.WriteLine ("This is the first" + Biao + "seed Buy Method: Shampoo" + x + "bottle, toothbrush" + y + "soap" + z + "block");
}
}
}
}
Console.WriteLine ("Total" + Biao + "kind of buy Method!") ");
Console.WriteLine (sum);
Console.ReadLine ();
Case
Badminton Racket 15 Yuan, the ball 3 yuan, the water 2 yuan. $200 each at least one, how much could it be
Set Racket x 200/15=20
Set the ball y 200/3=66
Set Water 200/2=100
int sum = 0;
for (int x = 1; x <= 20;x++)
{
for (int y = 1; y <= 66;y++)
{
for (int z = 1; z <= 100;z++)
{if (15*x+3*y+2*z==200)
{
sum++;
Console.WriteLine ("Badminton Racket" +x+ ", Ball" +y+ ", Water" +z+ "bottle");
}
}
}
} Console.WriteLine (sum);
Console.ReadLine ();
Case
Hundred Chicken Hundred Money: The Rooster 2 text Money One, the hen 1 money one, the chicken half article money one,
A total of only 100 cents, how to gather enough 100 chickens in the case of just spent 100 dollars? Each of at least one
Rooster set to G 100/2=50
Hen set to M 100/1=100
Chick set to x 100/0.5=200
Hundred Chicken Hundred Money: The Rooster 2 text Money One, the hen 1 money one, the chicken half article money one,
A total of only 100 cents, how to gather enough 100 chickens in the case of just spent 100 dollars?
Rooster set to G 100/2=50
Hen set to M 100/1=100
Chick set to x 100/0.5=200
int sum = 0;
for (int g = 0; G <=; g++)
{
for (int m = 0; M <=; m++)
{
for (int x = 0; x <=)
{
if (2 * g + 1 * m + 0.5 * x = = && g + m + x = = 100)
{
sum++;
Console.WriteLine ("Rooster" + G + "only," + "hen" + M + "only," + "chick" + x + "only");
}
}
}
}
Console.WriteLine (sum);
Console.ReadLine ();
While loop
It's actually a variant of the For loop.
for (int i = 1; i<=5;i++)
{
Circulation body;
}
The For loop above can be written as
int i= 1;
for (;i<=5;)
{
Circulation body;
i++;
}
Written while is the following style
int i= 1;
while (expression (i<=5))
{
Circulation body;
State change (i++);
}
Do
{
Circulation body;
State change (i++);
}while (expression (i<=5))
Note: The Do-while is a full-unsatisfied expression, and I'll do it again.
Jump statement:
Break: Jump out of the loop
Continue: Jump out of this cycle and continue the next cycle.
Iteration:
From the initial situation according to the law to constantly solve the intermediate situation, the final derivation of the results.
Case:
int a = 7;//884800000
int i = 1;
while (true)
{
A *= 2;
if (a >= 884800000)
{
Console.WriteLine (i);
Console.WriteLine (a);
Break
}
i++;
}
For poor lifting iterations