1、入口条件循环:程序进入循环体之前,先执行表达式的判断。若为真则开始执行循环体内的语句;若为假,则不执行循环体内的语句,越过循环体,执行循环体外的语句。
出口条件循环:首先执行循环体内的语句,然后再判断表达式。若表达式为真,则再次执行循环体,若为假,则退出循环,执行循环之后的语句。
出口条件循环:do...while循环
入口条件循环:whlie循环、for循环
2、
3、
4、 5、
6、
7、加大括号
8、表达式 int x=(1,024),这个表达式的右边由逗号运算符连接,因此 x 的值将被赋予 024,这是一个八进制形式,转换为十进制即为 20,也就是说 x 的值是20。而表达式 int y; y=1,024; 的意思是先将常量 1 赋给 y,而整个表达式的结果为 024,也就是 20,但结果并没有用而已。
9、cin>>ch 将跳过空白字符,比如换行符、空格以及制表符,而其它的两种形式都将读取这些字符。
二、编程练习
1、
#include<iostream>
using namespace std;
int main()
{
int i, j,min,max;
int sum=0;
cout << "请输入两个整数" << endl;
cin >> i >> j;
if(i>j)
{
max = i;
min = j;
}
else
{
max = j;
min = i;
}
while (min <= max)
{
sum += min;
min++;
}
cout << "两数之间的整数和为: " << sum << endl;
}
2、
#include<iostream>
#include<array>
using namespace std;
int main()
{
constexpr int ArSize = 100;
array <long double, 100>factor;
factor.at(1) = factor.at(0)= 1;
for (int i = 2; i < ArSize; i++)
factor.at(i) = i * factor.at(i-1);
for (int i = 0; i < ArSize; i++)
cout << i << " != " << factor.at(i) << endl;
}
3、
#include<iostream>
#include<array>
using namespace std;
int main()
{
int i;
int sum = 0;
cout << "请输入一个非零要加的数字" << endl;
cin >> i;
while (i!= 0)
{
sum += i;
cout << "请输入一个要加的数字(若输入零则程序停止)" << endl;
cin >> i;
}
cout << "所有数字累加和为: " << sum << endl;
}
4、
#include<iostream>
using namespace std;
int main()
{
double Dinterest =100, Cinterest =100;
int i = 0;
while (Dinterest>=Cinterest)
{
i++;
Dinterest = 0.1 * 100 * i + 100;
Cinterest = 0.05 * Cinterest+Cinterest;
}
cout << "第 " << i << " 年,Cleo的投资价值才能超过Daphne的投资价值" << endl;
cout << "此时Daphne的投资价值为" << Dinterest << endl;
cout << "此时Cleo的投资价值为" << Cinterest << endl;
}
5、
#include<iostream>
#include<string>
#include<array>
using namespace std;
int main()
{
array<int, 12>num = {0};
int all=0;
string month[12] = {"一月份","二月份","三月份" ,"四月份" ,"五月份" ,"六月份" ,"七月份" ,"八月份" ,"九月份","十月份","十一月份","十二月份"};
for (int i = 1; i <= 12; i++)
{
cout << i << "月份的销售量为:" << endl;
cin >>num.at(i-1);
all += num.at(i-1);
}
cout << "这一年的销售情况为:" << all << endl;
}
7.用vs编辑有超多警告
#include<iostream>
#include<string>
using namespace std;
struct car {
string name;
int years;
};
int main()
{
int num;
cout << "请输入有多少辆车" << endl;
cin >> num;
car *p= new car[num];
for (int i = 0; i <num; i++)
{
cout << "Car #" << i+1<<endl;
cout << "Please enter the make: ";
cin.ignore();
getline(cin, p[i].name);
cout << endl;
cout << "Please enter the year made: ";
cin >> p[i].years;
}
cout << "Here is your collection: " << endl;
for (int i = 0; i < num; i++)
{
cout << p[i].years << p[i].name << endl;
}
}
8、
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i=0;
const char arr[20] = "done";
char arr2[20];
while (strcmp(arr, arr2) != 0)
{
cout << "Enter words (to stop,type the word done): " << endl;
cin >> arr2;
cin.get();
i++;
}
cout << "You entered a total of " << i-1 << " words";
return 0;
}
9、编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "done";
string str2;
int counter = 0;
while (str1 != str2)
{
cout << "Enter words (to stop,type the word done):" << endl;
cin >> str2;
cin.get();
counter++;
}
cout << "You entered a total of " << counter - 1 << endl;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
....*
...**
..***
.****
****
#include<iostream>
using namespace std;
int main()
{
int i = 1,j=1,k=1,row;
cout << "Enter number of rows: " << endl;
cin >> row;
while (i <= row)
{
while (j <= row - i)
{
cout << ".";
j++;
}
j = 1;
while (k <= i)
{
cout << "*";
k++;
}
k = 1;
cout << endl;
i++;
}
return 0;
}