简介

C++语言提供了两种按照条件执行的语句:

  • if语句:根据条件决定控制流
  • switch语句:计算整型表达式的值,并根据这个值从几条执行路径中选择一条

if语句

和golang不同的是,C++语言中条件语句的condition必须用括号括起来。

  1. // 普通的if语句
  2. if (condition)
  3. statement
  4. // if-else语句
  5. if (condition)
  6. statement
  7. else
  8. statement2