Usage of C for statements

Source: Internet
Author: User

The for statement is a circular statement in c language. Next I will introduce some specific usage and examples of the for statement loop in c language to c beginners.

The for statement can initialize cyclic variables in expression 1.

[Example 6.7]

The Code is as follows: Copy code
Main ()
{
Int I, j, k;
Printf ("I j kn ");
For (I = 0; I <2; I ++)
For (j = 0; j <2; j ++)
For (k = 0; k <2; k ++)
Printf ("% d % dn", I, j, k );
}


In C, the for statement is the most flexible to use, and can replace the while statement. It generally takes the following form:

For (expression 1; expression 2; expression 3) Statement

The execution process is as follows:

First, solve expression 1.
Solution expression 2. If the value is true (not 0), execute the embedded statement specified in the for statement, and then execute step 3rd. If the value is false (0 ), end the loop and go to step 5th.
Solution expression 3.
Go back to step 2nd above to continue the execution.
When the loop ends, execute a statement below the for statement.
The execution process can be expressed.

The most simple and understandable form of for statements is as follows:
For statement
The initial value assignment of the cyclic variable is always a value assignment statement, which is used to assign an initial value to the cyclic control variable. The cyclic condition is a relational expression that determines when to exit the loop, defines how the cyclic control variables change after each loop. Separate the three parts. For example:
For (I = 1; I <= 100; I ++) sum = sum + I;

First assign the initial value 1 to I to determine whether I is less than or equal to 100. If yes, execute the statement and increase the value by 1. Then judge again until the condition is false, that is, when I> 100, the loop ends. Equivalent:

The Code is as follows: Copy code
I = 1;
While (I <= 100)
{Sum = sum + I;
I ++;
}

The general form of statements in the for loop is the following while LOOP form:
Expression 1;
While (expression 2)
{Statement
Expression 3;
}


Note the following when using the for statement:

In a for loop, "expression 1 (Initial Value of the loop variable)", "expression 2 (cycle condition)", and "expression 3 (increment of the loop variable)" Are all selected items, which can be defaulted, however, ";" cannot be the default value.
If expression 1 is omitted, the initial value is not assigned to the cyclic control variable.
If expression 2 (loop condition) is omitted, it becomes an endless loop without any other processing.
For example:

The Code is as follows: Copy code
For (I = 1; I ++) sum = sum + I;
Equivalent:
I = 1;
While (1)
{
Sum = sum + I;
I ++;
}

If expression 3 (loop variable increment) is omitted, loop control variables are not operated. You can add statements to the statement body to modify the loop control variables.
For example:

The Code is as follows: Copy code
For (I = 1; I <= 100 ;)
{Sum = sum + I;
I ++ ;}

"Expression 1 (initial value assigned to the cyclic variable)" and "expression 3 (incremental variable)" are omitted )".
For example:

The Code is as follows: Copy code
For (; I <= 100 ;)
{Sum = sum + I;
I ++ ;}
Equivalent:
While (I <= 100)
{Sum = sum + I;
I ++ ;}

All three expressions can be omitted.
For example:
For (;) Statement
Equivalent:
While (1) Statement

Expression 1 can be a value expression that sets the initial value of a loop variable, or other expressions.
For example:

The Code is as follows: Copy code
For (sum = 0; I <= 100; I ++) sum = sum + I;

Expressions 1 and 3 can be simple expressions or comma expressions.

The Code is as follows: Copy code
For (sum = 0, I = 1; I <= 100; I ++) sum = sum + I;
Or:
For (I = 0, j = 100; I <= 100; I ++, j --) k = I + j;

Expression 2 is generally a relational or logical expression, but it is also a numeric or character expression. As long as its value is not zero, the loop body is executed.
For example:

The Code is as follows: Copy code
For (I = 0; (c = getchar ())! = 'N'; I + = c );
Another example:
For (; (c = getchar ())! = 'N ';)
Printf ("% c", c );

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.