In some simple C + + programming, there are often two for (;;) Statement with the case, this is a common sentence pattern, so the feeling is also very important. Here are two examples to illustrate:
Example 1. With two for (;;) Statement to sort the size of an array element
#include "stdafx.h"
#include <iostream>
using namespace Std;
int main ()
{
int i,j,t=0;
int a[10]={0};
Enter 10 consecutive numbers in the array with a For loop
for (i=0;i<10;i++)
{
cin>>a[i];
}
Two for loop, sort input values in small to large order
for (i=0;i<10;i++)
for (j=i+1;j<10;j++)
{
if (A[i]>a[j])
{
T=a[i];
A[I]=A[J];
a[j]=t;
}
}
The sorted array values are output in order from small to large in a for loop
for (i=0;i<10;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
Example 2. A corridor is followed by N (1≤n≤65535) lamps, from beginning to end numbered 1, 2, 3 、... n-1, N. Each lamp is controlled by a pull-wire switch. At first, the lights were all closed. There were n students walking through the corridor. The first student pulls the switch of the electric light with a number of multiples of 1, then the second student pulls the switch of the light bulb, which is usually a multiple of 2, and the third student pulls the switch of the electric light with a number of multiples of 3; N students according to the provisions of the walk, there are several lights in the corridor. Note: The number of lights is the same as the number of students.
Program:
#include "stdafx.h"
#include <iostream>
#define MAX 65535
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
int n, I, j,cnt=0, A[max] = {0};
CIN >> N;
if ((N < 1) | | (N>max))
cout << "Input Error! "<< Endl;
for (i = 1; I <= n; i++)
{
for (j = 1; J <= N; j + +)
{
if ((j%i) = = 0)
{
A[J] + = 1;
A[J]%= 2;
}
}
}
for (i = 1; i<=n; i++)
{
if (a[i]==1)
cnt++;
}
cout << "Number of Lights" << cnt << "Light" << Endl;
return 0;
}
A corridor is followed by a
N (1
≤
N
≤
65535)
Lamp, numbered from beginning to end
1
、
2
、
3
、
... n
-1
、
N
。 Every
The lamp is controlled by a pull-wire switch. At first, the lights were all closed.
Yes
N
A student walked through the corridor. The first student has a number
1
A multiplier of the light switch pull;
A student puts the number
2
A multiple of the light switch pull up; then a third student
3
In multiples of
The switch of the light was pulled;
N
A student puts the number
N
The multiples of the light switch pull
A moment.
N
A student according to the provisions of the Walk, the corridor in the light of several lights.
Note: The number of lights is the same as the number of students.
A commonly used sentence pattern in C + + (two for (;;) The importance of the statement in conjunction)