The basic course of C language (iii) input and output functions and control flow statements (15)

Source: Internet
Author: User

1.3.2 Loop statement
The Turbo C2.0 provides three basic loop statements: for, while and do-while statements.

1.3.2.1 for Loop
The For loop is open. Its general form is:
For (< initialization >; < condition table over >; < increment >)
Statement
Initialization is always an assignment statement, which is used to assign an initial value to a circular control variable; a conditional expression is a relational expression that determines when to exit the loop, and the increment defines how the loop control variable changes after each loop. Between these three parts with ";" Separate.
For example:
for (I=1; i<=10; i++)
Statement
In the example above, I was given an initial value of 1 to determine if I was less than or equal to 10, and if the statement was executed, then it would increase by 1. Again, until the condition is false, that is, i>10, end the loop. Attention:
A statement in a 1.for loop can be a statement body, but the statements that participate in the loop are enclosed in "{" and "}".
The "initialization", "conditional expression", and "increment" in the 2.for loop are all selections, that is, you can default, but ";" cannot be defaulted. Initialization is omitted, indicating that no initial value is assigned to the loop control variable. If you omit a conditional expression, it becomes a dead loop without other processing. If the increment is omitted, the loop control variable is not manipulated, and the statement that modifies the loop control variable can be added to the statement body.
3.for loops can have multiple layers of nesting.
Example 16:
Main ()
{
int I, j, K;
printf ("I j k\n");
for (i=0; i<2; i++)
For (j=0 j<2; j + +)
for (k=0; k<2; k++)
printf (%d%d%d\n ", I, J, K);
}
The output results are:
I j K
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

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.