C # loop statement for loop (nested while exhaustive iteration ),

Source: Internet
Author: User

C # loop statement for loop (nested while exhaustive iteration ),

For loop nesting is similar to if else

Example:
Print the matrix. The outer loop corresponds to the row and the inner loop corresponds to the column.

For (int k = 1; k <= 5; k ++)
{
For (int I = 1; I <= 5; I ++)
{
Console. Write ("■ ");
}
Console. WriteLine ();
}


Print the right triangle in the lower left corner.
For (int I = 1; I <= 5; I ++)
{
For (int j = 1; j <= I; j ++)
{
Console. Write ("■ ");
}
Console. WriteLine ();
}

Print the triangle with a right angle in the upper left corner.
For (int I = 1; I <= 5; I ++)
{
For (int j = 5; j> = I; j --)
{
Console. Write ("■ ");
}
Console. WriteLine ();
}


Print the right triangle at the bottom right corner.
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 ();
}


Print the right triangle in the upper right corner.
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 ();
}

 


Enter a positive integer,
Print a number with the length of both Sides
Right triangle in the lower right corner
Console. Write ("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 the value is 1! + 2! +... + N!
Console. Write ("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 tip table
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

 

In fact, it is the deformation of the for loop.
For (int I = 1; I <= 5; I ++)
{
Loop body;
}
The above for loop can be written
Int I = 1;
For (; I <= 5 ;)
{
Loop body;
I ++;
}
Write while is the following style
Int I = 1;
While (expression (I <= 5 ))
{
Loop body;
Status Change (I ++ );
}

Do
{
Loop body;
Status Change (I ++ );
} While (expression (I <= 5 ))
Note: No matter whether the do while expression is full or not, I will execute it again first.

Example application:

Origami: 0.07mm, how many times the discount can exceed the height of Everest (8848 m)
Int ci = 0;
Double height = 0.07;
While (height <8848000)
{
Height * = 2;
Ci ++;
}

While (1 = 1)
{
Height * = 2;
Ci ++;
If (height & gt; = 8848000)
{
Break;
}
}
Console. WriteLine (ci );
Console. ReadLine ();


Use a while loop to create a 99 tip table
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 lifting

Use the if condition to filter out all possible conditions.

Example application:

 

The organization sent a 150 yuan shopping card,
Take the supermarket to buy three types of washing products.
Shampoo 15 yuan, soap 2 yuan, toothbrush 5 yuan.
How many purchase methods are there, and how many purchase methods are there?
Int sum = 0;
Int zong = 0;
For (int x = 0; x <= 10; x ++)
{
For (int y = 0; y <= 30; y ++)
{
For (int z = 0; z <= 75; z ++)
{
Zong ++;
If (x * 15 + y * 5 + z * 2 = 150)
{
Sum ++;
Console. WriteLine ("{0} purchase method: Shampoo {1} bottle, soap {2}, toothbrush {3. ", Sum, x, z, y );
}
}
}
}
Console. WriteLine ("Total" + sum + "purchase method. ");
Console. WriteLine (zong );
Console. ReadLine ();

 

Hundred chickens: Rooster 2, hen 1, and chicken half
Int a = 0;
For (int g = 0; g <= 50; g ++)
{
For (int m = 0; m <= 100; m ++)
{
For (int x = 0; x <= 200; x ++)
{
If (g + m + x = 100 & g * 2 + m + x * 0.5 = 100)
{
Console. WriteLine (g + "Rooster" + m + "hen" + x + "chicken ");
A ++;
}
}
}
}
Console. WriteLine ();
Console. ReadLine ();

 

Iteration

Based on the initial conditions, the intermediate conditions are solved according to the regular rules, and the results are finally exported.

Example application:

The paper can be folded infinitely. The thickness of the paper is 0.07mm.
How many times can the discount exceed 8848 meters?
8848 meters = 8848000
Double height = 0.07;
Int ci = 0;
For (;;)
{
Height * = 2;
Ci ++;
Console. WriteLine (ci + "times, the current height is:" + height/1000 + "meters. ");
If (height & gt; = 8848000)
{
Break; // jump out of the entire Loop
}
}
Console. ReadLine ();

 


Five children lined up and asked how old the first one was,
The first one is two years older than the second one and asks how old the second one is,
The second one is two years older than the third one... And so on,
I asked 5th children that they were 3 years old. How old is the first child?

Int m = 3;
For (int I = 1; I <= 4; I ++)
{
M + = 2;
}
Console. WriteLine ("the first child is {0} years old. ", M );
Console. ReadLine ();

 


Big Horse camel 2 stone grain, medium horse camel 1 stone grain, two horse camel 1 stone grain,
How should we allocate 100 horses and 100 camels of grain?
For (int d = 0; d <= 100; d ++) // d is a Trojan
{
For (int z = 0; z <= 100; z ++) // z indicates
{
For (int x = 0; x <= 100; x ++) // z is the pony
{
If (d + z + x = 100 & 2 * d + z + 0.5 * x = 100)
{
Console. WriteLine ("horse" + d + "horse, horse" + z + "horse, Pony" + x + "horse ");
}
}
}
}
Console. ReadLine ();

 

4. coins with 1 cent, 2 cent, and 5 cent,

How many combinations are required for a combination of 1.5 yuan?
Int n = 0;
For (int a = 0; a <= 150; a ++)
{
For (int B = 0; B <= 75; B ++)
{For (int c = 0; c <= 30; c ++)
{
If (a + 2 * B + 5 * c = 150)
{
Console. WriteLine ("1 cent" + a + "2 cent" + B + "3 cent" + c );
N ++ ;}
}
}
} Console. WriteLine ("combination methods" + n );
Console. ReadLine ();

 

Related Article

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.