C++ if 语句

C++ 教程 · 2019-02-25 15:56:56

一个 if 语句 由一个布尔表达式后跟一个或多个语句组成。

语法

C++ 中 if 语句的语法:

if(boolean_expression)
{
   // 如果布尔表达式为真将执行的语句
}

如果布尔表达式为 true,则 if 语句内的代码块将被执行。如果布尔表达式为 false,则 if 语句结束后的第一组代码(闭括号后)将被执行。

C 语言把任何非零非空的值假定为 true,把null 假定为 false

流程图

C++ 中的 if 语句

实例

#include <iostream>
using namespace std;
 
int main ()
{
   // 局部变量声明
   int a = 10;
 
   // 使用 if 语句检查布尔条件
   if( a < 20 )
   {
       // 如果条件为真,则输出下面的语句
       cout << "a 小于 20" << endl;
   }
   cout << "a 的值是 " << a << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

a 小于 20
a 的值是 10

点击查看所有 C++ 教程 文章: https://codercto.com/courses/l/18.html

查看所有标签

Introduction to Linear Optimization

Introduction to Linear Optimization

Dimitris Bertsimas、John N. Tsitsiklis / Athena Scientific / 1997-02-01 / USD 89.00

"The true merit of this book, however, lies in its pedagogical qualities which are so impressive..." "Throughout the book, the authors make serious efforts to give geometric and intuitive explanations......一起来看看 《Introduction to Linear Optimization》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码