Statement-for () Loop statement-iterative method

Source: Internet
Author: User

for () loop

for (int i=1/* initial condition */;i<=100/* cycle condition */;i++/* State change */)

Four elements: initial conditions, cyclic condition, state change, circulation body.

Execution process: Initial conditions-cycle condition-loop body-state change-cycle condition ....

Note: For parentheses, do not add a semicolon after the parentheses of the for.

For can be nested.

Application: Iterative method, poor lifting method

First, the iterative method: there are certain rules.

Each cycle is obtained from the results of the last operation, the result of this operation is to prepare for the next operation.

Cases:

1.100 or less of all numbers.

1

2

3

4

5

6

7

int sum = 0;

for (int i = 1; i <=; i++)

{

sum = sum + i;

}

Console.WriteLine ("The sum of the number of the numbers you enter is" + sums);

2. There is a monkey and a bunch of peaches in the park, and the monkeys eat half of the peaches every day and throw away a bad one in half. By the seventh day, the monkey opened his eyes and found that there was only a peach left. Q. How many peaches are there in the park at first?

int TZ = 1;

for (int i = 6; I >= 1; i--)

{

TZ = (TZ + 1) * 2;

}

Console.WriteLine ("The total number of peaches for the first day is" + TZ + "one");

3, five children lined up in a team. Ask the first how old, the first to say two years older than the second, ask the second, the second one is two years older than the third, and so on. Ask the fifth child how old, the fifth child said 3 years old. Q How old is the first child?

int j = 3;

for (int i = 4; I >= 1; i--)

{

j = j + 2;

}

Console.WriteLine ("First child" + j + "age");

4, a sheet of paper thickness is 0.1 mm, assuming this paper can be limited to fold, ask how many times can be more than Everest? 8848 m

int a = 1;

Double z = 0.0001;

for (int i = 1; I <= i+1; i++)

{

z = z * 2;

if (Z > 8848)

{

A = i;

Break

}

}

Console.WriteLine ("Fold" + A + "times can be as high as Mt. Everest");

5, the board put food (please put 1 grain in the first lattice, the second lattice put 2, the third lattice put four grains, and so on, if 1 grain weight 0.1 grams, then the board 30 lattice grain total weight how many grams?

int lszl = 1;

for (int i = 1; i <; i++)

{

int x = 1;

for (int j = 1; J <= i;j++)

{

x = x * 2;

}

Lszl = Lszl + x;

}

Double ZL = Lszl * 0.01;

Console.WriteLine ("+zl+" kg "required to fill the board);

6. The blue ball falls from 5 meters high, the height of each bounce is the last half, ask the height of the nth bounce?

Console.WriteLine ("Please enter the number of times the basketball bounces:");

int n = Convert.ToInt32 (Console.ReadLine ());

Double gd=5;

for (int g = 1; G <=n; g++)

{

GD=GD/2;

if (g = = 5)

{

GD = 0;

}

}

Console.WriteLine ("The first" + N + "The height of the second bounce is" + gd + "M");

Statement-for () Loop statement-iterative method

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.