Zerglurker C-Language tutorial 008--looping statements

Source: Internet
Author: User

Last lesson we talked about the three major execution sequences of program statements: Sequential execution, conditional execution, and cyclic execution

Today we talk about loop execution, no nonsense, first on the code and run the effect

First, a sample code, the usual, a new empty project (refer to Lesson 006)

Create a new empty main.cpp and public.h

Public.h inside the definition code is as follows:

void largedistributionsequence (unsigned int nlength);

The main.cpp code is as follows:

#include <stdio.h> #include "public.h" int main (int nargc, char* argv[], char* env[]) {largedistributionsequence (15 ); return 0;} void largedistributionsequence (unsigned int nlength) {unsigned i;for (i = 0; i < nlength;i++) {if (I! = 0 && ((i %) = = 0) {printf ("\ n");} printf ("% 4d", ((i + 1) * (i + 1) & (To))/2);} printf ("\ n");}

The effect after Ctrl+f5 is as follows:


The use format for the FOR statement is this:

for (initialization statement; conditional statement; processing of changing conditions for each cycle) requires a single statement of the loop;

for (initialization statement; Conditional statement; Change condition processing per loop) {

Multiple statements that need to be looped;

}

For loop a few points to note:

1 initialization statements can be written without writing, or you can write multiple

Like what

for (;i<nlength;)
Or

for (i=0,j=0;i<nlength;i++,j++)
Note that when multiple initialization statements are written together, they must be a comma-concatenated expression

An expression rather than a statement

2 change condition processing of each cycle can also not write or write multiple, write rules with the above

See 1 for examples

3 conditional statements must be written and judged at the beginning of each loop

For the first loop, the conditional judgment is immediately executed when the initialization statement is completed.

If the judgment fails, the subsequent statement is not executed

4 suggest that even if you need a circular statement, you can use braces in multiple ways.

This will not cause the whole logic to change greatly because of your statement.

Like what:

for (i=0;i<nlength;i++)    printf ("ok\n"), if (i==nlength) printf ("no\n");
If the requirements change, not carefully, the second line of the statement is commented out, then the whole logic will become a repeat judgment I is equal to nlength, and then nothing will output

Comment One line, starting with the symbol, followed by a comment, and the compiler ignores

/* Comment Content */comment a block from/* behind, to/front, all the contents will be considered as comments, the compiler will ignore

We often use annotations to temporarily remove some unused functionality, or add some code descriptions

In addition to the for can implement loops, we can also use while to implement the loop

After adding the code in Public.h, the following:

void largedistributionsequence (unsigned int nlength); void largedistributionsequencewhile (unsigned int nlength); void Largedistributionsequencedowhile (unsigned int nlength);
Add the following code to the main.cpp:

#include <stdio.h> #include "public.h" int main (int nargc, char* argv[], char* env[]) {largedistributionsequence (15 ); Largedistributionsequencewhile (15); Largedistributionsequencedowhile (); return 0;} void largedistributionsequence (unsigned int nlength) {unsigned i;for (i = 0; i < nlength;i++) {if (I! = 0 && ((i %) = = 0) {printf ("\ n");} printf ("% 4d", ((i + 1) * (i + 1) & (To))/2);} printf ("\ n");} void largedistributionsequencewhile (unsigned int nlength) {unsigned i=0;while (i<nlength) {if (I! = 0 && ((i% 10 ) = = 0) {printf ("\ n");} printf ("% 4d", ((i + 1) * (i + 1) & (To))/2); i++;} printf ("\ n");} void largedistributionsequencedowhile (unsigned int nlength) {Unsigned i = 0;do{if (i! = 0 && ((i%) = = 0)) {print F ("\ n");} printf ("% 4d", ((i + 1) * (i + 1) & (To))/2); i++;} while (I < nlength);p rintf ("\ n");}
After the run, the results are as follows:


As you can see, three ways result in exactly the same

The while is used in the following way:

while (conditional) single statement;

while (condition) {

More than one statement;

}

Do single statement, while (condition);

do{

More than one statement;

}while (conditions);

Like for the attention of 3,while I also recommend that you use curly braces to enclose code as much as possible.

Another benefit of curly braces is that even if a statement is not expanded, it does not cause the loop logic to affect the logic outside of the loop, a precaution that can save your life in hundreds of lines of code.

Otherwise those inexplicable program errors, even let you do not know how to feel the world collapsed.

The for was originally designed to perform well-defined loops, i.e. the initial conditions are clear, the cyclic termination condition is clear, and the variable-definite loops

While the while is used for fuzzy loops. For example, I do not know the initial conditions or only partial initial conditions. The variable is also ambiguous, it may change, or the next loop will change, or the user's specific input will be changed

But when actually used, they can be replaced by each other.

For example, when the initial condition is not clear, for the initialization statement I can empty out, write only conditional statements. If the variable is not clear, I can not write the change condition of each cycle to handle

Similarly, for while I can write the initialization statement at the outside of the while and handle variable changes within the while

However, there is a definite difference between the while and the Do...while:

That is, Do...while must first execute the loop body, regardless of whether the condition is established. While when the condition is not set, it may not be executed once

So the above test code will change all 15 in main to 0, then the output will look like this:


The third time, the output of a 0

printf can format output, as seen above: printf ("% 4d", ((i + 1) * (i + 1) & (To))/2);

% indicates formatting information followed by a space indicating that the fill symbol is a space, 4 for the alignment length, and d for the output of an integer

The whole meaning is to output integers in 4-character alignment, where the blanks are filled with insufficient space

So the output of the numbers is much more beautiful.

However, once the number exceeds 4 characters in length, it is output as is, breaking the format

About the output format, which I'll cover in detail later

In addition, in the C language, the For initialization statement cannot declare a variable

That is, the variable must first be declared, and then used

In C + +, the FOR initialization statement can declare variables, but different versions of the compiler handle different

The variable declared by the for initialization statement in VC6, the scope is as large as for. A for initializes a declared variable that can be used in the code of the for peer

In the case of C99 and later compilers, this is not allowed, and the variables declared in the for are only used in for, but not visible outside

Zerglurker C-Language tutorial 008--looping statements

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.