This is a Clilstore unit. You can .

To control loops two statements *break* and *continue* are used. They extend the capabilities of loop application and improve the structure of the program.

*break -* performs stoppage of loop execution; the control is passed to the statement following the loop.

*continue- *performs completion of execution of the next step of the loop before the end of the loop will be reached. Loop step takes the following value.

Statement *break * is written in the loop body, i.e. the structure of the loop body will be as follows:

*{*

*statements;*

*if (condition) break;*

*}*

__Example____ ____27____.__ Given n integers. Find their product. If the number is 0, then the exit from the program is executed, using *break* command.

main()

{int i, n, x, P=1;

cin>>n; //quantity of numbers

for (i=1; i<=n; i++)

{ cin>>x; // enter of numbers

if (x==0) break;

P*=x;}

cout<<”P=”<<P;

}

__Example____ __* 28.* Given n integers. Display the index of the first number, which is exactly divisible by 5.

main()

{int i, n, x;

cin>>n;

for (i=1; i<=n; i++)

{ cin>>x;

if (x%5==0) break;}

cout<<”index of the first number, which is divisible by 5=”<<i;

}

When using statement *continue, *the loop body will have the following structure:

*{*

*statements;*

*if (condition) continue;*

*}*

Consider the examples of use of statement *Continue*.

* Example29. *Calculate the sum and quantity of positive numbers.

main()

{int i, n, x, k, S;

k=S=0;

cin>>n;

for (i=1; i<=n; i++)

{ cin>>x;

if (x<0) continue;

S+=x; k++; }

cout<<”S=”<<S<<”k=”<<k;

}

__Example____ ____30__*.* Find the arithmetic average of even numbers.

{int i, n, x, S=0, k=0;

cin>>n;

for (i=1; i<=n; i++)

{ cin>>x;

if (x%2==1) continue;

S+=x; k++; }

cout<<”Arithmetic average”<<S/k;

}

Short url: http://test.multidict.net/cs/6249