循环和关系表达式
for循环:
for循环的组成部分
for循环为执行操作提供了循序渐进的步骤:
- 设置初始值
- 执行测试,看看循环是否应当进行
- 执行循环操作
- 更新用于测试的值
C++将整个for看作一条语句,虽然循环体可以包含一条或多条语句(包含多条语句时,需要使用复合语句或代码块{})
循环只执行一次初始化。for循环是入口条循环,这意味着在每轮循环之前,都将计算测试表达式的值。更新表达式在每轮循环结束时执行,此时循环体已经执行完毕。
提示:C++常用的方式是,在for和括号之间加一个空格,而省略函数名与括号之间的空格。
在for中声明变量,这种变量只存在于for语句中,也就是说,当程序离开循环后,这种变量将消失。
//for--阶乘
#include<iostream>
const int ArSize = 16; //example of external declaration
int main()
{
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
std::cin.get();
return 0;
}
//for--访问字符串
#include<iostream>
#include<string>
int main()
{
using namespace std;
cout << "Enter a word";
string word;
cin >> word;
//display letters in reverse order
for (int i = word.size() - 1; i >= 0; i--) //注意长度,注意字符串数组下标
cout << word[i];
cout << "\nbye\n";
cin.get();
cin.get();
return 0;
}
++和—
a++意味着使用a的当前值计算表达式,然后将a的值加1;而++b的意思是先将b的值加1,然后使用新的值来计算表达式。
不要在同一条语句对同一个变量递增或递减多次。
前缀递增和前缀递减和解除引用运算符*的优先级相同,以从右到左的方式进行结合。后缀递增和后缀递减的优先级相同,但比前缀运算符的优先级高,这两个运算符以从左到右的方式进行结合。(运用在指针上时需要注意)
关系表达式
<,<=,==,>,>=,!=
在关系表达式中,如果比较结果为真,则其值将为true,否则为false
while循环
while循环是没有初始化和更新部分的for循环,它只有测试条件和循环体:
while(test-condition)
body
首先,程序计算圆括号内的表达式。如果该表达式为true,则执行循环体中的语句。与for循环一样,循环体也由一条语句或两个花括号定义的语句块组成。执行完循环体后,程序返回测试条件对它重新评估。
//编写演延时循环
#include<iostream>
#include<ctime> //decribes clock() function,clock t type
int main()
{
using namespace std;
cout << "Enter the delay time, in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC; //convert to clock ticks
cout << "start...\n";
clock_t start = clock();
while (clock() - start < delay)
; //note the semicolon
cout << "done\n";
cin.get(); //前面只要有cin输入,结尾要两个cin.get()来使窗口不闪退
cin.get();
return 0;
}
类型别名
typedef typeName aliasName;
do while循环
不同于前面介绍的两种循环,使出口条件,先执行循环体,然后再判定测试表达式
do
body
while(test-expression);
//do while
#include<iostream>
int main()
{
using namespace std;
int n;
cout << "Enter numbers in the range 1-10 to find: ";
cout << "My favorite number.\n";
do
{
cin >> n; //execute body
} while (n != 7); //the test
cout << "Yes! 7 is my favorite number.";
cin.get();
cin.get();
return 0;
}
基于范围的for循环(C++11)
C++11新增了一种循环:基于范围的for循环。这简化了一种常见的循环任务,对数组(或容器类,如vector和array)的每个元素执行相同的操作。
double price[5] = { 2.1,2.3,3.4,5.6,8.9 };
for (double x : price)
cout << x << endl;
其中x表示数组的第一个元素。显示第一个元素后不断执行循环,而x依次表示其他元素
要想修改数组元素,需要使用不同的循环变量语法:
double &x,符号&表示x是一个引用变量,这种声明让接下来的代码可以修改数组元素。
还可以结合使用基于范围的for循环初始化列表。
嵌套循环和二维数组
int maxtemps[4][5] =
{
{89,89,98,99,87},
{78,98,65,78,99},
...
}; //注意,一定要用分号结束一句话
#include<iostream>
const int Cities = 5;
const int Years = 4;
int main()
{
using namespace std;
const char* cities[Cities]= //array of pointers to 5 strings
{
"北京",
"上海",
"广州",
"深圳",
"武汉",
};
int maxtemps[Years][Cities] =
{
{39,39,40,42,41},
{ 39,39,40,42,41 },
{ 39,39,40,42,41 },
{ 39,39,40,42,41 },
};
cout << "Maximum temperatures for 2008-2011\n\n";
for (int city = 0; city < Cities; ++city)
{
cout << cities[city] << " : \t";
for (int year = 0; year < Years; ++year)
cout << maxtemps[year][city] << "\t";
cout << endl;
}
cin.get();
return 0;
}
在输出中使用制表符比使用空格可使数据排列更有规则!
在这个例子中,也可以使用char数组char[Cities][25];但从存储空间来说使用指针更经济;另外还可以使用string对象数组而不是字符串指针数组。