练习题
1.int i;
for(i = 0; i < 5; i++)
cout << i;
cout << endl;
这段代码将输出:
01234
2.int j;
for(j = 0; j < 11; j += 3)
cout << j;
cout << endl << j << endl;
这段代码将输出:
0369
12
3.int j = 5;
while(++j < 9)
cout << j++ << endl;
这段代码将输出:
6
8
4.int k = 8;
do
cout << "k = " << k << endl;
while(k++ < 5);
这段代码将输出:
k = 8;
5.
#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;
int main()
{
int result = 1;
for (int i = 0; i < 7; i++)
{
cout << result << " ";
result *= 2;
}
}
6.
#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int total = 0;
cout << "Enter a number:";
cin >> a;
cout << "Enter another number that larger than first one:";
cin >> b;
for (; a <= b; a++) {
total += a;
}
cout << "The sum of integers between two numbers is:" << total << endl;
}
7.
#include <iostream>
#include <cstdio>
#include <string>
#include <array>
using namespace std;
int main()
{
int temp = 0;
int sum = 0;
cout << "Enter a number(Enter zero mean quit program):";
cin >> temp;
while (temp != 0) {
sum += temp;
cout << "Current sum is:" << sum << endl;
cout << "Enter a number(Enter zero mean quit program):";
cin >> temp;
}
}