The for loop details of C ++ are mandatory !, For mandatory

Source: Internet
Author: User

The for loop details of C ++ are mandatory !, For mandatory

In C ++, The For (A; B; C) C statement is executed after each loop.

For example, y = 10;
For (I = 0; I <10; y = ++ I)
{
Cout <y <endl;
}

The value assignment operation is performed on y only after one loop, so the first output is 10!


Explanation of C for loop statements

# Include <stdio. h>
Void main ()
{
Int a, B, c, d = 0;
For (a = 1; a <5; a ++)
{
For (B = 1; B <5; B ++)
{For (c = 1; c <5; c ++)
{If (! = B & B! = C &! = C)
{
D ++;
Printf (\ "% d \", a, B, c );
}
}
}
}
Printf (\ "\ n \");
Printf (\ "there are % d such numbers \ n \", d );
}

Remember to write all the brackets for beginners.

How can I explain the multiple cycles of C language for to my younger brother in detail?

For (expression 1; expression 2; expression 3)

Statement:

Expression 1 is usually used to assign an initial value to a loop variable. It is generally a value assignment expression. You can also assign an initial value to the loop variable outside the for statement. This expression can be omitted.

Expression 2 is usually a circular condition, usually a relational expression or a logical expression.

Expression 3 is usually used to modify the value of a loop variable. It is generally a value assignment statement.

All three expressions can be comma expressions, that is, each expression can be composed of multiple expressions. All three expressions are optional and can be omitted.

The "statement" in the general form is a loop body statement. The syntax of the for statement is:

1. Calculate the value of expression 1 first.

2. Calculate the value of expression 2. If the value is true (not 0), execute the loop body once. Otherwise, the loop jumps out.

3. Then, calculate the value of expression 3 and go back to Step 3 for repeated execution. During the for loop process, expression 1 is calculated only once. Expression 2 and expression 3 may be calculated multiple times. The loop body may be executed multiple times or not at all. The execution process of the for statement.

Void main (){
Int n, s = 0;
For (n = 1; n <= 100; n ++)
S = s + n;
Printf ("s = % d \ n", s );
}

Use the for statement to calculate s = 1 + 2 + 3 +... + 99 + 100

Int n, s = 0;
For (n = 1; n <= 100; n ++)
S = s + n;
Printf ("s = % d \ n", s );

In this example, expression 3 in the for statement is n ++, which is actually a value assignment statement, equivalent to n = n + 1, to change the value of the loop variable.

Void main (){
Int a = 0, n;
Printf ("\ n input n :");
Scanf ("% d", & n );
For (; n> 0; a ++, n --)
Printf ("% d", a * 2 );
}

Use the for statement to modify the example. Output n consecutive even numbers starting from 0.

Int a = 0, n;
Printf ("\ n input n :");
Scanf ("% d", & n );
For (; n> 0; a ++, n --)
Printf ("% d", a * 2 );

In this example, expression 1 is omitted in the for statement, and the initial value of the loop variable is obtained by the scanf statement before the for statement. Expression 3 is a comma expression, composed of a ++, n is composed of two expressions. Each cycle increases a by 1 and n by 1. A changes increase the output even number, and n changes control the number of cycles.

Note the following when using the for statement:

1. All expressions in the for statement can be omitted, but there must be no fewer semicolons. For example, for (; expression) saves expression 1. For (expression; expression) removes expression 2.

For (expression;) removes expression 3. For (;) removes all expressions.

2. When the initial value of the cyclic variable has been assigned, expression 1 can be omitted. In example, 3.27 is the case. If expression 2 or expression 3 is omitted, an infinite loop is generated. In this case, the loop should be ended in the loop body. This is the case.

Void main (){
Int a = 0, n;
Printf ("\ n input n :");
Scanf ("% d", & n );
For (; n> 0 ;)
{A ++; n --;
Printf ("% d", a * 2 );
}
} Int a = 0, n;
Printf ("\ n input n :");
Scanf (& quo... the remaining full text>

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.