c ++ first personal work

C ++ loop structure is important for realizing the structure of the code used repeatedly

Three different and nested loop structure is used must be capable of understanding

Three loop structures are:

 Can be added to the initial conditions, the cycling conditions and parameters in parentheses for the for loop. Such that the entire loop looks clear and concise.

Its expression is for (Expression 1 (may be omitted); Expression 2; Expression 3 (may be omitted)), for example:

include <iostream>
using namespace
int main()
{
    int a;
    for(a=1;a<=10;a++)
    {
        cout<<"a="<<a<<endl;
    }
    return 0;
}

 

do while the while loop will need to be added in the parameter variation control body, wherein the execution condition is determined before the previous execution will do while, while the determination is performed first and then, whether to continue execution.

Using the format: do while (termination condition), while (termination condition).

E.g:

#include <iostream>

using namespace

int main()
{
    int a=1,
    do while(a<1)
    {
        a++;
    }
    cout<<"a="<<a<<endl;
    a=1;
    while(a<1)
    {
        a++;
    }
    cout<<"a="<<a<<endl;
    return 0;
}

Nested loop to follow the three innermost takes precedence, with variables defined in the cycle can not be used in the outer layer.

#include <iostream>
using namespace
int main()
{
    int a,b,c,sum;
    for(a=1,b=1,c=1;a<10;a++)
    {
        do while(b<10)
        {
            while(c<10)
            {
                sum=a*b*c;
                cout<<"sum="<<sum<<endl;
            }
        }
    }
}

Reuse code not only can avoid a lot of trouble, you can also make features of the software can smoothly progress

Guess you like

Origin www.cnblogs.com/genm-one/p/11518862.html