MCU Learning: C-Language Foundation--for Loop and for nesting

Source: Internet
Author: User

==========for Loop and for nesting =========-For loop Syntax format: for (expression 1; expression 2; expression 3) {loop-body;} Function: When entering a for statement, execute expression 1 first (only once!) ), and then to the expression 2 true and false judgment, if True (if False then Exit For loop), then execute the loop body content, after the execution of expression 3, expression 3 after the execution, the expression 2 is re-false judgment. For example:

#include <stdio.h>
int main ()
{
int x;
for (x=0; x<5; x + +)
{
printf ("x =%d\n", i);
}
return 0;
}

The above program outputs the results in vc++6.0:

x = 0
x = 1
x = 2
x = 3
x = 4
Press any key to continue

Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

==========for Nesting ===========

When the For loop body is also a for statement, it is called A For loop nesting

For example:

for (expression 1; expression 2; expression 3) {for (expression A; expression b; expression b) {loop body 2; }} instance:

#include <stdio.h>
#define UCHAR unsigned char
#define UINT unsigned int
int main ()
{
Uchar x, y;
for (x=0; x<5; x + +)
{
printf ("x =%d,", x);
for (y=0; y<3; y++)
{
printf ("y =%d,", y);
}
printf ("\ n");
}
return 0;
}

The above program outputs the results in vc++6.0:

x = 0,y = 0,y = 1,y = 2,
x = 1,y = 0,y = 1,y = 2,
x = 2,y = 0,y = 1,y = 2,
x = 3,y = 0,y = 1,y = 2,
x = 4,y = 0,y = 1,y = 2,
Press any key to continue

This shows that x changes every time, y changes 3 times

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.