现代 C++:精华部分/现在开始有意思了。
外观
	
	
到目前为止,我们所有的程序都很直接:从顶部开始,逐行读取,直到到达底部。现在情况发生了变化,因为这不是编写程序的真正有用方式。有用的程序通常会重复一些东西,或者决定不做一些东西。本章将介绍如何在运行时选择执行路径。
#include <iostream>
#include <string>
int main()
{
	std::string input;
	int a, b;
	int greater;
	// This one starts out with a value.
	bool canDecide = true;
	std::cout << "Enter two integers to compare.\n";
	// Take input.
	std::cin >> input;
	// Parse it as an int.
	a = std::stoi(input);
	
	// Take input.
	std::cin >> input;
	// Parse it as an int.
	b = std::stoi(input);
	
	// This is equality, and it might be true now and false later.
	if(a == b)
	{
		// = does not mean equality, which means we can do this.
		canDecide = false;
	}
	else if (a < b)
	{
		greater = b;
	}
	else
	{
		greater = a;
	}
	
	if(canDecide)
	{
		std::cout << greater << " is the greater number.\n";
	}
	else
	{
		std::cout << "The numbers are equal.";
	}
}
bool 是 布尔 的缩写,表示是或否、真或假。在本例中,布尔变量 "canDecide" 初始化为 true。基本上,程序假设它 "可以决定" 哪个数字更大,直到看到相反的证据。以这种方式覆盖布尔值非常有用。
== 是相等运算符;它确定两个值是否相等,并返回真或假。注意:它不会改变任何值的的值。
任何看起来像 if(...){...} 的都是一个 if 语句。a == b 是一个 表达式,它计算为布尔值。表达式是一些可以解析为值的代码。if(a == b) 后的花括号包含如果 a 等于 b 则运行的代码。
else if 表示 "否则,如果...",并且只有在第一个 if 语句的 条件(布尔值)为假时才会测试它。else 本身不会测试任何条件,而是在其链中的任何 if 语句都没有运行时才会运行。
当以这种方式选择一些逻辑进行执行时,"控制" 被认为 "流入" 逻辑。因此,if 语句被认为是 控制流 语句或 控制结构,它是一组包含一些其他构造,这些构造将在后面介绍。
- 编写一个计算器,提示用户输入两个浮点数和一个运算符,然后打印结果。
- bool
- 两个可能值之一,true或false。也称为 条件。
- 表达式
- 一些可以解析为值的代码。
- if 语句
- 如果其条件为真则运行。语法:if(condition){ }
- 控制流
- 哪些代码运行,以及运行顺序。
- 控制结构
- 修改控制流的语句。也称为 控制流语句。