采用括号、对齐、空格、换行有助于更清晰的表达复杂的逻辑表达式。
示例:
#include <iostream>
using namespace std;
int main() {
// 天气的数据:温度(单位:摄氏度),天气状况(晴朗;多云;下雨;下雪)
int temperature = 25; // 温度
string weather = "晴朗"; // 天气状况
// 理想的户外活动条件:温度在20到30度之间,且天气是晴朗或多云。
if ((temperature >= 20 && temperature <= 30) && // 温度条件
(weather == "晴朗" || weather == "多云")) { // 天气条件
cout << "今天是进行户外活动的好天气!\n";
} else {
cout << "今天的天气可能不适合进行户外活动。\n";
}
return 0;
}