Saturday, December 18, 2010
【 Weak current College 】 MCU c language tutorial-lesson eight: the statement (5)-loop 】
Circulation statement is almost every program is used, it is used to realize the need to repeatedly for several actions. As one of the 51 chip 12M application circuit requires implementation of 1 millisecond delay, do 1000 times the empty statement before they can reach delay purpose (of course you can use timers to do here is not to discuss), if you are writing 1000 empty statement how troublesome thing, besides it is necessary to take up a lot of storage space. We can know the 1000 empty statement, is nothing more than an empty statements repeat 1000 times, so we can write a loop statement, this will not only make the program structure, clarity, and the compilation of efficiency is greatly improved. In the C language constitutes a loop control statements, for while, do-while and goto statements. The same is to cycle, but the specific role and usage is quite different. We specifically look at. The GoTo statement
This statement in many advanced language will have to remember when using the BASIC liked using this statement. It is an unconditional steering statement, as long as the execution of this statement, the program pointer will jump to the label after the goto is located in the program section. Its syntax is as follows:
The GoTo statement labels;
The statement label for an identifier with a colon. Example: void main (void)
{
unsigned char a;
start: a++;
if (a==10) goto end;
goto start;
end:;
A section of the program above the} can be said to be a dead loop, nothing, just to clarify the use of goto. This program is the beginning of the program with the identifier "start:" identity, said that this is the start of the program, "end:" identifies the end of the program, the definition of the identifier should follow the preceding definition of the identifier, you cannot use the C keyword cannot also be and other variable and function names are the same, otherwise an error occurs. A c++ program execution, a value of 1 when a is equal to 10, the program will skip to the end at the end of the program identified, or jump back to a start ID Department continue until a c++ is equal to 10. The above example shows unconditional goto can not only, but you can turn and if statements constitute a loop structure, those in the C programmer's programs are less common, common usage is to use a goto statement to jump out of multiple loop, but it can jump from the inner loop of the outer loop, not from the outer loop, jump to the innermost loop. In the following for loop statement to again slightly to mention. Why most c programmers don't like to use the GoTo statement? it is because too many use it when the program structure is not clear that too many jump makes the program returned to compile programming style, make the program lost C modular advantages.
Labels:
[:]
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment