C++ Tutorial/Operators statements/for loop

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A conversion table of feet to meters

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 double f; // holds the length in feet  
 double m; // holds the conversion to meters  
 int counter; 

 counter = 0; 

 for(f = 1.0; f <= 100.0; f++) { 
   m = f / 3.28; // convert to meters 
   cout << f << " feet is " << m << " meters.\n";  
    
   counter++;  

   // every 10th line, print a blank line 
   if(counter == 10) { 
     cout << "\n"; // output a blank line 
     counter = 0; // reset the line counter 
   } 
 } 

 return 0; 

}</source>

1 feet is 0.304878 meters.
2 feet is 0.609756 meters.
3 feet is 0.914634 meters.
4 feet is 1.21951 meters.
5 feet is 1.52439 meters.
6 feet is 1.82927 meters.
7 feet is 2.13415 meters.
8 feet is 2.43902 meters.
9 feet is 2.7439 meters.
10 feet is 3.04878 meters.
11 feet is 3.35366 meters.
12 feet is 3.65854 meters.
13 feet is 3.96341 meters.
14 feet is 4.26829 meters.
15 feet is 4.57317 meters.
16 feet is 4.87805 meters.
17 feet is 5.18293 meters.
18 feet is 5.4878 meters.
19 feet is 5.79268 meters.
20 feet is 6.09756 meters.
21 feet is 6.40244 meters.
22 feet is 6.70732 meters.
23 feet is 7.0122 meters.
24 feet is 7.31707 meters.
25 feet is 7.62195 meters.
26 feet is 7.92683 meters.
27 feet is 8.23171 meters.
28 feet is 8.53659 meters.
29 feet is 8.84146 meters.
30 feet is 9.14634 meters.
31 feet is 9.45122 meters.
32 feet is 9.7561 meters.
33 feet is 10.061 meters.
34 feet is 10.3659 meters.
35 feet is 10.6707 meters.
36 feet is 10.9756 meters.
37 feet is 11.2805 meters.
38 feet is 11.5854 meters.
39 feet is 11.8902 meters.
40 feet is 12.1951 meters.
41 feet is 12.5 meters.
42 feet is 12.8049 meters.
43 feet is 13.1098 meters.
44 feet is 13.4146 meters.
45 feet is 13.7195 meters.
46 feet is 14.0244 meters.
47 feet is 14.3293 meters.
48 feet is 14.6341 meters.
49 feet is 14.939 meters.
50 feet is 15.2439 meters.
51 feet is 15.5488 meters.
52 feet is 15.8537 meters.
53 feet is 16.1585 meters.
54 feet is 16.4634 meters.
55 feet is 16.7683 meters.
56 feet is 17.0732 meters.
57 feet is 17.378 meters.
58 feet is 17.6829 meters.
59 feet is 17.9878 meters.
60 feet is 18.2927 meters.
61 feet is 18.5976 meters.
62 feet is 18.9024 meters.
63 feet is 19.2073 meters.
64 feet is 19.5122 meters.
65 feet is 19.8171 meters.
66 feet is 20.122 meters.
67 feet is 20.4268 meters.
68 feet is 20.7317 meters.
69 feet is 21.0366 meters.
70 feet is 21.3415 meters.
71 feet is 21.6463 meters.
72 feet is 21.9512 meters.
73 feet is 22.2561 meters.
74 feet is 22.561 meters.
75 feet is 22.8659 meters.
76 feet is 23.1707 meters.
77 feet is 23.4756 meters.
78 feet is 23.7805 meters.
79 feet is 24.0854 meters.
80 feet is 24.3902 meters.
81 feet is 24.6951 meters.
82 feet is 25 meters.
83 feet is 25.3049 meters.
84 feet is 25.6098 meters.
85 feet is 25.9146 meters.
86 feet is 26.2195 meters.
87 feet is 26.5244 meters.
88 feet is 26.8293 meters.
89 feet is 27.1341 meters.
90 feet is 27.439 meters.
91 feet is 27.7439 meters.
92 feet is 28.0488 meters.
93 feet is 28.3537 meters.
94 feet is 28.6585 meters.
95 feet is 28.9634 meters.
96 feet is 29.2683 meters.
97 feet is 29.5732 meters.
98 feet is 29.878 meters.
99 feet is 30.1829 meters.
100 feet is 30.4878 meters.

A for loop with no increment

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 int x; 

 for(x=0; x != 123; ) { 
   cout << "Enter a number: "; 
   cin >> x; 
 } 

 return 0; 

}</source>

Enter a number: 2
Enter a number: 3
Enter a number: 2
Enter a number: 123

A negatively running for loop

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 int i; 

 for(i=50; i >= -50; i = i-10) 
    cout << i << " "; 

 return 0; 

}</source>

50 40 30 20 10 0 -10 -20 -30 -40 -50 "

Compound interest calculations with for

<source lang="cpp">#include <iostream> using std::cout; using std::endl; using std::fixed;

  1. include <iomanip>

using std::setw; using std::setprecision;

  1. include <cmath>

using std::pow; int main() {

  double amount;
  double principal = 1000.0;
  double rate = .05; 
  cout << "Year" << setw( 21 ) << "Amount on deposit" << endl;
  cout << fixed << setprecision( 2 );
  for ( int year = 1; year <= 10; year++ ) 
  {
     amount = principal * pow( 1.0 + rate, year );
     cout << setw( 4 ) << year << setw( 21 ) << amount << endl;
  }
  return 0;

}</source>

Year    Amount on deposit
   1              1050.00
   2              1102.50
   3              1157.63
   4              1215.51
   5              1276.28
   6              1340.10
   7              1407.10
   8              1477.46
   9              1551.33
  10              1628.89

Declare loop control variable inside the for

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 int sum = 0; 
 int fact = 1; 

 for(int i = 1; i <= 5; i++) {  
   sum += i;  // i is known throughout the loop 
   fact *= i; 
 } 

 // but, i is not known here. 

 cout << "Sum is " << sum << "\n"; 
 cout << "Factorial is " << fact; 

 return 0; 

}</source>

Sum is 15
Factorial is 120"

Display the alphabet: use char to control for loop

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 char letter; 

 for(letter = "A"; letter <= "Z"; letter++) 
   cout << letter; 

 return 0; 

}</source>

ABCDEFGHIJKLMNOPQRSTUVWXYZ"

empty for loop statement

<source lang="cpp">#include <iostream>

int main()
{
    int counter=0;      
    int max = 3;
    for (;;)   
    {
        if (counter < max) 
        {
            std::cout << "Hello!\n";
            counter++;          // increment
        }
        else
            break;
    }
    return 0;
}</source>
Hello!
Hello!
Hello!

Floating point control in a for loop

<source lang="cpp">#include <iostream>

  1. include <iomanip>

using std::cout; using std::endl; int main() {

 const double pi = 3.14;
 cout << endl;
 for(double radius = .2 ; radius <= 3.0 ; radius += .2)
   cout << "radius = " << std::setw(6) << radius
        << "  area = " << std::setw(12) << pi * radius * radius
        << endl;
 return 0;

}</source>

radius =    0.2  area =       0.1256
radius =    0.4  area =       0.5024
radius =    0.6  area =       1.1304
radius =    0.8  area =       2.0096
radius =      1  area =         3.14
radius =    1.2  area =       4.5216
radius =    1.4  area =       6.1544
radius =    1.6  area =       8.0384
radius =    1.8  area =      10.1736
radius =      2  area =        12.56
radius =    2.2  area =      15.1976
radius =    2.4  area =      18.0864
radius =    2.6  area =      21.2264
radius =    2.8  area =      24.6176

For loops with null statements

<source lang="cpp">#include <iostream>

int main()
{
    int counter = 0;

    for( ; counter < 5; )
    {
        counter++;
        std::cout << "Looping!  ";
    }

    std::cout << "\nCounter: " << counter << ".\n";
    return 0;
}</source>
Looping!  Looping!  Looping!  Looping!  Looping!
Counter: 5.

Generating multiplication tables

<source lang="cpp">#include <iostream>

  1. include <iomanip>
  2. include <cctype>

using std::cout; using std::cin; using std::endl; using std::setw; int main() {

 int table = 11;
 const int table_min = 2;                      
 const int table_max = 12;                     
 // Create the top line of the table
 cout << "     |";
 for(int i = 1 ; i <= table ; i++)
   cout << " " << setw(3) << i << " |";
 cout << endl; 
 
 // Create the separator row
 for(int i = 0 ; i <= table ; i++)
   cout << "------";
 cout << endl;
 
 for(int i = 1 ; i <= table ; i++) {   
   cout << " " << setw(3) << i << " |";
 
   // Output the values in a row
   for(int j = 1 ; j <= table ; j++)
     cout << " " << setw(3) << i*j << " |"; 
   cout << endl;                     
 }
 
 
 return 0;

}</source>

|   1 |   2 |   3 |   4 |   5 |   6 |   7 |   8 |   9 |  10 |  11 |
------------------------------------------------------------------------
   1 |   1 |   2 |   3 |   4 |   5 |   6 |   7 |   8 |   9 |  10 |  11 |
   2 |   2 |   4 |   6 |   8 |  10 |  12 |  14 |  16 |  18 |  20 |  22 |
   3 |   3 |   6 |   9 |  12 |  15 |  18 |  21 |  24 |  27 |  30 |  33 |
   4 |   4 |   8 |  12 |  16 |  20 |  24 |  28 |  32 |  36 |  40 |  44 |
   5 |   5 |  10 |  15 |  20 |  25 |  30 |  35 |  40 |  45 |  50 |  55 |
   6 |   6 |  12 |  18 |  24 |  30 |  36 |  42 |  48 |  54 |  60 |  66 |
   7 |   7 |  14 |  21 |  28 |  35 |  42 |  49 |  56 |  63 |  70 |  77 |
   8 |   8 |  16 |  24 |  32 |  40 |  48 |  56 |  64 |  72 |  80 |  88 |
   9 |   9 |  18 |  27 |  36 |  45 |  54 |  63 |  72 |  81 |  90 |  99 |
  10 |  10 |  20 |  30 |  40 |  50 |  60 |  70 |  80 |  90 | 100 | 110 |
  11 |  11 |  22 |  33 |  44 |  55 |  66 |  77 |  88 |  99 | 110 | 121 |

Loop until a random number that is greater than

<source lang="cpp">#include <iostream>

  1. include <cstdlib>

using namespace std;

int main() {

 int i;  
 int r; 
 

 r = rand(); 

 for(i=0; r <= 20000; i++) 
   r = rand(); 
    
 
 cout << "Number is " << r << 
   ". It was generated on try " << i << "."; 
 
 return 0;  

}</source>

Number is 26500. It was generated on try 3."

Simplest for loop statement

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 int count; 

 for(count=1; count <= 100; count=count+1)  
   cout << count << " "; 

 return 0; 

}</source>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5
7 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 "

The body of a for loop can be empty

<source lang="cpp">#include <iostream>

  1. include <cstdlib>

using namespace std;

int main() {

 int i;  
 int sum = 0; 
 

 // sum the numbers from 1 through 10 
 for(i=1; i <= 10; sum += i++) ; 
 
 cout << "Sum is " << sum; 

 return 0;  

}</source>

Sum is 55"

Use multiple statements in for loops

<source lang="cpp">#include <iostream>

int main()
{
    for (int i=0, j=0; i<3; i++, j++)
        std::cout << "i: " << i << " j: " << j << std::endl;
    return 0;
}</source>
i: 0 j: 0
i: 1 j: 1
i: 2 j: 2

Use nested for loops to find factors of numbers between 2 and 100

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 for(int i=2; i <= 100; i++) { 
   cout << "Factors of " << i << ": "; 

   for(int j = 2; j < i; j++) 
     if((ij) == 0) cout << j << " "; 

   cout << "\n"; 
 } 

 return 0; 

}</source>

Factors of 2:
Factors of 3:
Factors of 4: 2
Factors of 5:
Factors of 6: 2 3
Factors of 7:
Factors of 8: 2 4
Factors of 9: 3
Factors of 10: 2 5
Factors of 11:
Factors of 12: 2 3 4 6
Factors of 13:
Factors of 14: 2 7
Factors of 15: 3 5
Factors of 16: 2 4 8
Factors of 17:
Factors of 18: 2 3 6 9
Factors of 19:
Factors of 20: 2 4 5 10
Factors of 21: 3 7
Factors of 22: 2 11
Factors of 23:
Factors of 24: 2 3 4 6 8 12
Factors of 25: 5
Factors of 26: 2 13
Factors of 27: 3 9
Factors of 28: 2 4 7 14
Factors of 29:
Factors of 30: 2 3 5 6 10 15
Factors of 31:
Factors of 32: 2 4 8 16
Factors of 33: 3 11
Factors of 34: 2 17
Factors of 35: 5 7
Factors of 36: 2 3 4 6 9 12 18
Factors of 37:
Factors of 38: 2 19
Factors of 39: 3 13
Factors of 40: 2 4 5 8 10 20
Factors of 41:
Factors of 42: 2 3 6 7 14 21
Factors of 43:
Factors of 44: 2 4 11 22
Factors of 45: 3 5 9 15
Factors of 46: 2 23
Factors of 47:
Factors of 48: 2 3 4 6 8 12 16 24
Factors of 49: 7
Factors of 50: 2 5 10 25
Factors of 51: 3 17
Factors of 52: 2 4 13 26
Factors of 53:
Factors of 54: 2 3 6 9 18 27
Factors of 55: 5 11
Factors of 56: 2 4 7 8 14 28
Factors of 57: 3 19
Factors of 58: 2 29
Factors of 59:
Factors of 60: 2 3 4 5 6 10 12 15 20 30
Factors of 61:
Factors of 62: 2 31
Factors of 63: 3 7 9 21
Factors of 64: 2 4 8 16 32
Factors of 65: 5 13
Factors of 66: 2 3 6 11 22 33
Factors of 67:
Factors of 68: 2 4 17 34
Factors of 69: 3 23
Factors of 70: 2 5 7 10 14 35
Factors of 71:
Factors of 72: 2 3 4 6 8 9 12 18 24 36
Factors of 73:
Factors of 74: 2 37
Factors of 75: 3 5 15 25
Factors of 76: 2 4 19 38
Factors of 77: 7 11
Factors of 78: 2 3 6 13 26 39
Factors of 79:
Factors of 80: 2 4 5 8 10 16 20 40
Factors of 81: 3 9 27
Factors of 82: 2 41
Factors of 83:
Factors of 84: 2 3 4 6 7 12 14 21 28 42
Factors of 85: 5 17
Factors of 86: 2 43
Factors of 87: 3 29
Factors of 88: 2 4 8 11 22 44
Factors of 89:
Factors of 90: 2 3 5 6 9 10 15 18 30 45
Factors of 91: 7 13
Factors of 92: 2 4 23 46
Factors of 93: 3 31
Factors of 94: 2 47
Factors of 95: 5 19
Factors of 96: 2 3 4 6 8 12 16 24 32 48
Factors of 97:
Factors of 98: 2 7 14 49
Factors of 99: 3 9 11 33
Factors of 100: 2 4 5 10 20 25 50