First, the Class C language mentioned here refers to the same programming language as C, C + +, C #, and Java, as well as the C language or similar. In these languages, the syntax of the FOR statement and the execution flow are the same. This article will discuss the usage of this statement in a more in-depth.
For statement:
Copy Code code as follows:
for (expression 1; expression 2; expression 3)
{
Loop statement
}
Expression 1 assigns an initial value to a cyclic variable
Expression 2 is a cyclic condition
Expression 3 is used to modify the value of a loop variable, called a circular step.
The execution process for the FOR statement:
Example: Programming calculation: The result of 1+2+3+...+99+100.
This is the cumulative problem, the general expression of the cumulative problem is: s = s + T;
wherein, S is a variable, which is used to store the cumulative final result, called an accumulator; t is an expression that represents the value of each time you need to add an accumulator.
Accumulation is generally achieved through the cyclic structure. The initial value of the accumulator s is set to 0 before looping.
An additive t can be a very simple expression, or it may be a more complex expression obtained through analysis.
int S = 0;
for (int i = 1; I <= i++)
{
s = s + i;
}
printf ("%d\n", S);
Attention:
1 for the problem that has been determined to be an accumulation algorithm, the relationship between the additive and the cyclic variable is first determined.
2) Then, consider the value of the first additive to determine the initial values of the cyclic variable;
3 Second, determine the value of the last additive, and determine the cyclic conditions;
4 Finally, consider the relationship between each additive to determine the change of the cyclic variable (step value).
Example: Programming calculation s = a + AA + AAA + AAAA + ... The sum of the first n items.
For example, if a = 3,n = 5, the expression is: S = 3 + 33 + 333 + 3333 + 33333.
int S = 0;
int t = 0;
for (int i = 0;i < 5;i++)
{
t = T * ten + 3;
s = s + t;
}
printf ("%d\n", S);
The cumulative algorithm can also evolve into a multiplicative form: S = 1 * 2 * 3 * ... * n.
The algorithm of multiplication is similar to the additive algorithm, and the loop body realizes the form of S = S * t. Note that the initial value of S is not 0, but to be set to 1.
You should be aware of an overflow problem when you multiply, because each time the result grows very fast.
Circulation Procedure Design Idea:
(1) What is the input? Used to determine the original value.
(2) What is the output? Determine the results that should be output.
(3) What steps are needed to convert from input to output, whether it needs to be repeated (to determine whether a loop statement is needed and the contents of a looping statement).
(4) If you need to repeat, can you predict the number of repetitions in advance? Used to determine which loop statement to use while, do, and for.
(5) When should no longer be repeated? It is used to determine the cyclic condition and the statement that the loop condition is approximated to 0 in the loop body.
Examples: The number of daffodils, the number of four-leaf rose, the number of five-pointed stars, six digits.
Narcissus number: The sum of 3 powers per digit equals itself. Example: 1^3 + 5^3 + 3^3 = 153.
Four-leaf rose number: the sum of 4 powers per digit equals itself.
Five-star number: The sum of 5 powers per digit equals itself.
Six digits: The sum of the 6 powers of each digit equals itself.
Print out all the daffodils count
int i,j,k,n,s;
for (i = 1;i <= 9;i++) for (
j = 0;j <= 9;j++) for
(k = 0;k <= 9;k++)
{
n = k * k * * k + J * J * j + I * I * i;
s = i * + j * + K;
if (n = = s)
{
printf ("%d\n", n);
}
}
OR:
//Print out all narcissus number
int i,j,k,n,s;
printf ("Narcissus number: \ n");
for (n = 100;n < 1000;n++)
{
i = n/100; /* Break out hundred
/j = n/10%; /* Decompose 10 bits
/k = n%; /* Decomposition of a single-digit
/s = i * I * i + J * J * j + K * k * k;
if (s = = N)
{
printf ("%d\n", n);
}
}
Finally, I use the example to sum up! The following example is an example of a sum of 1-10 of these 10 numbers.
Copy Code code as follows:
int i, sum = 0, num = 10; Declare several variables: I count, sum store the final sum, num represents how many numbers, used to control the boundary.
for (i = 1; I <= num; i++)//A For statement contains three statements, separated by;
{//1 Sets the initial value, 2 boundary control, 3 is usually used to adjust the count value.
sum + = i; equal to sum = sum + i; I'm constantly adding I. The statements within {} are loop bodies, expressed in 4.
The order of execution of the/For loop is 1-2-4-3-2-4-3-2-4-3-2 ... until i > num. At this point, the loop is exited.
printf ("The sum from 1", sum); Print output sum value.