A For loop is nested similar to if else
Case:
Print matrix
for (int k = 1; k <= 5; k++)
{
for (int i = 1; I <= 5; i++)
{
Console.Write ("");
}
Console.WriteLine ();
}
Print triangle with right angle at bottom left
for (int i = 1; I <= 5; i++)
{
for (int j = 1; J <= I; j + +)
{
Console.Write ("");
}
Console.WriteLine ();
}
The upper left corner is a rectangular triangle
for (int i = 1; I <= 5; i++)
{
for (int j = 5; J >= i; j--)
{
Console.Write ("");
}
Console.WriteLine ();
}
The bottom right corner of the print triangle is rectangular
for (int i = 1; I <= 5; i++)
{
for (int k = 1; k <= 5-i; k++)
{
Console.Write ("");
}
for (int j = 1; J <= I; j + +)
{
Console.Write ("");
}
Console.WriteLine ();
}
The top right corner of the print triangle is rectangular
for (int i = 1; I <= 5; i++)
{
for (int k = 2; k <= i; k++)
{
Console.Write ("");
}
for (int j = 5; J >= i; j--)
{
Console.Write ("");
}
Console.WriteLine ();
}
Please enter a positive integer,
Print a two-sided length of this number based on this number.
A triangle with a right angle in the lower right-hand corner
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
for (int i = 1; i <= A; i++)
{
for (int j = 1; J <= A-i; j + +)
{
Console.Write ("");
}
for (int k = 1; k <= i; k++)
{
Console.Write ("");
}
Console.WriteLine ();
}
Console.ReadLine ();
Enter an integer and ask for 1! +2! +...+n!
Console.Write ("Please enter a positive integer:");
int a = Int. Parse (Console.ReadLine ());
int sum = 0;
for (int i = 1; i <= A; i++)
{
int jie = 1;
for (int j = 1; J <= I; j + +)
{
Jie *= J;
}
sum + + Jie;
}
Console.WriteLine (sum);
99 Table of formulas
for (int i = 1; I <= 9; i++)
{
for (int j = 1; J <= I; j + +)
{
Console.Write (j+ "*" +i+ "=" +j*i+ "T");
}
Console.WriteLine ();
}
Console.ReadLine ();
While loop
is actually a for-loop variant.
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++);
}
Todo
{
Circulation body;
State change (i++);
}while (expression (i<=5))
Note: I will execute it first, regardless of whether the expression is full or not.
Example application:
Origami: 0.07mm, how many times in fold can exceed Everest height (8848m)
int ci = 0;
Double height = 0.07;
while (height < 8848000)
{
Height *= 2;
ci++;
}
while (1 = 1)
{
Height *= 2;
ci++;
if (height >= 8848000)
{
Break
}
}
Console.WriteLine (CI);
Console.ReadLine ();
Use a while loop to do 99 tables
int i = 1;
while (I <= 9)
{
int j = 1;
while (J <= i)
{
Console.Write (j+ "*" +i+ "=" +j*i+ "T");
j + +;
}
Console.WriteLine ();
i++;
}
Console.ReadLine ();
Poor lift
Go through all the possible situations and use the IF condition to filter out the conditions that satisfy the condition.
Example application:
Unit to send a 150-dollar shopping card,
Take to the supermarket to buy three kinds of washing supplies.
Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan.
Ask just spend 150 yuan, how many kinds of buy method, each kind of buy method is each buy a few kind?
int sum = 0;
int zong = 0;
for (int x = 0; <= x + +)
{
for (int y = 0; y <= y++)
{
for (int z = 0; z <= z++)
{
zong++;
if (x * + y * 5 + z * 2 = 150)
{
sum++;
Console.WriteLine ("Article {0}" Buy Method: Shampoo {1} bottle, soap {2} block, toothbrush {3} branch.) ", sum,x,z,y);
}
}
}
}
Console.WriteLine ("There is a total of" +sum+ "kind of buying law. ");
Console.WriteLine (Zong);
Console.ReadLine ();
Iterations
From the initial situation, according to the law of the continuous solution of the intermediate situation, finally deduced results.
Example application:
The paper can be folded infinitely and the paper thickness is 0.07 mm.
How many times can you fold at least 8848 meters in half?
8848 m =8848000
Double height = 0.07;
int ci = 0;
for (;;)
{
Height *= 2;
ci++;
Console.WriteLine (CI + "times, now the height is:" + height/1000 + "M. ");
if (height >= 8848000)
{
break;//jump out of the loop
}
}
Console.ReadLine ();
Five kids lined up and asked how big the first one was,
The first one says two years older than the second, and asks how big the second one is,
The second one says two years older than the third. Analogy
Asked the 5th child, said he was 3 years old. How old are you when you ask the first child?
int m = 3;
for (int i = 1; I <= 4; i++)
{
m + + 2;
}
Console.WriteLine ("The first child {0} years old.") ", m);
Console.ReadLine ();
Great horse camel 2 grain, medium horse camel 1 stone grain, two ponies 1 stone grain,
How to allocate 100 horses and 100 stone grains?
for (int d=0;d<=100;d + +)//d for big Horse
{
for (int z=0;z<=100; z++)//z as Medium horse
{
for (int x=0;x<=100; x + +)//z for ponies
{
if (d +z+x==100&&2*d+z+0.5*x==100)
{
Console.WriteLine ("Big Horse" +d+ "horse", Middle horse "+z+" horse ", pony" +x+ "horse");
}
}
}
}
Console.ReadLine ();
4. There are 1 cents, 2 cents, 5 cents of coins,
To combine 1.5 yuan of money, there are several combinations of ways, how many respectively?
int n = 0;
for (int a = 0; a <= a++)
{
for (int b = 0; b <=-b++)
{for (int c=0;c<=30;c++)
{
if (A + 2 * b + 5 * c = n)
{
Console.WriteLine ("1 Cents" +a+ "penny" +b+ "Three Cents" +c);
n++;}
}
}
}console.writeline ("combination has" +n);
Console.ReadLine ();