1.下面的代码会打印什么内容
int i;
for(int i=0;i<5;i++)
{
cout<<i;
cout<<endl;
}
输出结果:
0
1
2
3
4
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
最后一句如果是cout<<++j<<endl;
则输出结果为
7
9
4.下面的代码会打印什么内容。
int k=8;
do
{
cout<<"k ="<<k<<endl;
}
while(k++<5);
输出结果:
k=8
do while语句先循环一遍,再看符不符合循环条件是否跳出循环
5.编写一个打印1 2 4 8 16 32 64的for循环。
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
for (int i = 1; i <65; i *= 2)
cout << i << " ";
return 0;
}
6.编写一个程序,让用户输入两个整数,输出这两个整数之间所有整数的和。
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
int i, j;
int s = 0;
cout << "请输入第一个整数" << endl;
cin >> i;
cout << "请输入第二个整数" << endl;
cin >> j;
if (i < j)
{
for (; i < j + 1; i++)
{
s += i;
cout << i << " ";
}
}
else
{
for (; j < i + 1; j++)
{
s += j;
cout << j << " ";
}
}
cout << endl << "总和为:" << s << endl;
return 0;
}
7.编写一个程序,让用户可以连续输入数字,每次输入数字,报告当前所有输入的和,当用户输入输入0的时候,程序结束。
#include "pch.h"
#include <iostream>
#include "stdio.h"
using namespace std;
int main()
{
int i, s;
cout << "请输入值" << endl;
cin >> i;
s = i;
cout << "当前和为:" << s << endl;
while (i != 0)
{
cout << "请输入值" << endl;
cin >> i;
s += i;
cout << "当前和为:" << s << endl;
}
return 0;
}