Single chip microcomputer C language Tutorial: C51 Loop statement

Source: Internet
Author: User
Tags goto

SCM C Language Tutorial 13th lesson C51 Loop Statement

Loop statements are used by almost every program, and are used to implement operations that require multiple iterations. such as a 12M 51 chip application circuit requires the implementation of 1 milliseconds delay, then the execution of 1000 empty statements to achieve the purpose of delay (of course, can use the timer to do, here is not discussed), if it is to write 1000 empty statements that is how troublesome things, and the other is to occupy a lot of storage space. We can know that these 1000 empty statements, nothing more than an empty statement repeated 1000 times, so we can use the Loop statement to write, so that not only the program

The structure is clear, and the efficiency of compiling it is greatly improved. The statements that make up the loop control in the C language have while,do-while,for and goto statements. It is also a cyclical function, but the specific role and use of the method is very different. Let's take a look at it specifically.

Goto statement

This statement will be available in many high-level languages, and it is a good idea to use this statement when you were a child with BASIC. It is an unconditional turn statement, as long as it executes to this statement, the program pointer jumps to the program segment where the label is located after the Goto. Its syntax is as follows:

Goto statement label; Where the statement designator is a colon-delimited identifier. Examples such as the following

void Main (void)

{

unsigned char A;

start:a++;

if (a==10) goto end;

Goto start;

End:;

}

The above procedure simply illustrates the use of Goto, which is seldom used in practical writing. The meaning of this procedure

is at the beginning of the program with the identifier "Start:" identity, indicating that the program is the beginning of the program, "End:" To identify the end of the program, the definition of the identifier should follow the above-mentioned identifier definition principle, cannot use the C keyword and other variables and function name, otherwise it will be wrong. The program executes the value of A++,a plus 1, and when a equals 10 o'clock the program skips to the end of the program, otherwise jumps back to the start identifier to continue a++ until a equals 10. The above example shows that Goto can not only unconditionally turn, but also can and if statements form a looping structure, which is less common in C Programmer's program, the common Goto statement uses it to jump out of multiple loops, but it can only from the inner loop jump to the outer loop, not from the outer loop to jump to the inner loop. Let me mention a little more about the FOR loop statement below. Why do most C programmers not like to use goto statements? That's because the program structure is not clear when you use it too much,

Too many jumps on the program back to the assembly of the programming style, so that the program lost the advantages of the modular C.

While statement

The meaning of the while statement is not difficult to understand, in English it means "when ... ", here we can understand that" when the condition is true, execute the following statement ", its syntax is as follows:

while (conditional expression) statement;

When using the while statement, be aware that when the conditional expression is true, it executes the following statement, and then returns

To the while execution condition judgment, the true time repeats executes the statement, exits the loop body when false. When the condition is false at first, then the loop body (statement or compound statement) after the while will exit the loop without executing it once. When you debug a program, you

Note that while the judgment condition can not be false caused by the dead loop, debugging appropriate in the while to add breakpoints, may make your debugging work more smoothly. Of course, sometimes use the dead loop to wait for interrupts or IO signals, such as in the first article we used while (1) to output "Hello world! ”。 The following example shows the cumulative sum from 1 to 10, and the reader can modify the condition in the while to see if the result will be, thus, to experience the use of the while.

#include

#include

void Main (void)

{

unsigned int I = 1;

unsigned int SUM = 0; Set initial value

SCON = 0x50; Serial port Mode 1, allows receiving

Tmod = 0x20; Timer 1 Timing Mode 2

TCON = 0x40; Set Timer 1 Start count

TH1 = 0xe8; 11.0592MHz 1200 baud rate

TL1 = 0xe8; TI = 1;

TR1 = 1; Start timer

while (i<=10)

{

sum = I + sum; Accumulation

printf ("%d sum=%d\n", i,sum); Show

i++;

}

while (1); This is to not let the program after the program pointer continues to cause the program to "Run To fly"

}

The final running result is sum=55;

Do While statement

The Do While statement is a complement to the while statement, while the while is the first to determine whether the condition is set up and then executes the loop body,

The Do while executes the loop body and then determines whether to exit the loop based on the condition. This determines whether the loop body will be executed at least once in any condition. Its syntax is as follows:

Do statement while (conditional expression)

How do you write the above routine with do? Think first, and then refer to the following procedure.

#include

#include

void Main (void)

{

unsigned int I = 1;

unsigned int SUM = 0; Set initial value

SCON = 0x50; Serial port Mode 1, allows receiving tmod = 0x20; Timer 1 Timing Mode 2

TCON = 0x40; Set Timer 1 Start count

TH1 = 0xe8; 11.0592MHz 1200 baud Rate TL1 = 0xe8;

TI = 1;

TR1 = 1; Start timer

Do

{

sum = I + sum; Accumulation

printf ("%d sum=%d\n", i,sum); Display i++;

}

while (i<=10);

while (1);

}

In the above program it appears that the do and while statements do not seem to be the same, but in practical applications it is

The loop body of any do while is bound to be executed once. If I set the initial value of the above two programs to 11, then the previous program will not get the results, and then a program will be sum=11.

For statement

In the case of a clear loop, the For statement is easier than the above-mentioned loop statement. Its syntax is as follows: for ([initial value setting expression];[ Cyclic condition expression]; [conditional update expression]) The expression in parentheses in the statement is optional, so that the for statement changes a lot. For statement execution: first

Substituting the initial value, and then determine whether the condition is true, the condition is satisfied when the loop body and update conditions, and then determine whether the condition is true ... Exits the loop until the condition is false. The following example is to achieve the same as in the previous two examples, compared to see not difficult to understand the differences in several loop statements.

#include

#include

void Main (void)

{

unsigned int I;

unsigned int SUM = 0; Set initial value

SCON = 0x50; Serial port Mode 1, allows receiving tmod = 0x20; Timer 1 Timing Mode 2

TCON = 0x40; Set Timer 1 Start count

TH1 = 0xe8; 11.0592MHz 1200 baud Rate TL1 = 0xe8;

TI = 1;

TR1 = 1; Start timer

for (I=1; i<=10; i++)//The initial value can be set here, so the variable definition can not be set

{

sum = I + sum; Accumulation

printf ("%d sum=%d\n", i,sum); Show

}

while (1);

}

If we change the for (; i<=10; i++) The initial value of this condition will be changed to the current I variable

Value. If you change to for (;;) What's going to happen? Try.

Continue statements

The continue statement is a statement used for interrupts, usually used in a loop, to end the loop, skip statements that are not executed in the loop body, and jump to the next cycle. The syntax is:

Continue

Continue is also an unconditional jump statement, but the function is different from the break statement mentioned earlier, continue does not jump out of the loop, but jumps to the beginning of the loop and executes the next loop. In the above example the loop body joins if (i==5) continue; see what results?

Return statement

The return statement is a returned statement, not a looping statement, which is the last statement to learn, so write it down. The return statement is used to end the execution of the function, returning to the position where the function was called. There are two types of syntax:

return (expression);

Return The syntax is returned with an expression, the expression is evaluated first, and the value of the expression is returned. Returned without an expression.

Value is not determined.

The following is an addition to the calculation 1-10, which is not the same as the way the function is used.

#include

#include

int Count (void); declaring functions

void Main (void)

{

unsigned int temp;

SCON = 0x50; Serial port Mode 1, allows receiving tmod = 0x20; Timer 1 Timing Mode 2

TCON = 0x40; Set Timer 1 Start count

TH1 = 0xe8; 11.0592MHz 1200 baud Rate TL1 = 0xe8;

TI = 1;

TR1 = 1; Start timer

temp = Count ();

printf ("1-10 sum=%d\n", temp); Show

while (1);

}

int Count (void)

{

unsigned int I, SUM;

for (I=1; i<=10; i++)

{

sum = I + sum; Accumulation

}

return (SUM);

}

Single chip microcomputer C language Tutorial: C51 Loop statement

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.