c语言中的if语句

Conditional code flow is the ability to change the way a piece of code behaves based on certain conditions. In such situations you can use if statements.

条件代码流是根据某些条件更改一段代码的行为的能力。 在这种情况下,可以使用if语句。

The if statement is also known as a decision making statement, as it makes a decision on the basis of a given condition or expression. The block of code inside the if statement is executed is the condition evaluates to true. However, the code inside the curly braces is skipped if the condition evaluates to false, and the code after the if statement is executed.

if语句也称为决策语句,因为它根据给定的条件或表达式进行决策。 if条件评估为true,则执行if语句中的代码块。 但是,如果条件的计算结果为false,则会跳过花括号内的代码,并执行if语句之后的代码。

if语句的语法 (Syntax of an if statement)

if (testCondition) {// statements
}

一个简单的例子 (A simple example)

Let’s look at an example of this in action:

让我们看一个实际的例子:

#include <stdio.h>
#include <stdbool.h>int main(void) {if(true) {printf("Statement is True!\n");}return 0;
}

Output:

输出:

Statement is True!

If the code inside parenthesis of the if statement is true, everything within the curly braces is executed. In this case, true evaluates to true, so the code runs the printf function.

如果if语句括号内的代码为true,则执行花括号中的所有内容。 在这种情况下, true评估为true,因此代码将运行printf函数。

if..else语句 (if..else statements)

In an if...else statement, if the code in the parenthesis of the if statement is true, the code inside its brackets is executed. But if the statement inside the parenthesis is false, all the code within the else statement's brackets is executed instead.

if...else语句中,如果if语句括号中的代码为true,则执行括号内的代码。 但是,如果括号内的语句为false,则执行else语句括号内的所有代码。

Of course, the example above isn't very useful in this case because true always evaluates to true. Here's another that's a bit more practical:

当然,上面的示例在这种情况下不是很有用,因为true总是评估为true。 这是另一个更实用的方法:

#include <stdio.h>int main(void) {int n = 2;if(n == 3) { // comparing n with 3printf("Statement is True!\n");} else { // if the first condition is not true, come to this block of codeprintf("Statement is False!\n");}return 0;
}

Output:

输出:

Statement is False!

There are a few important differences here. First, stdbool.h hasn’t been included. That's okay because true and false aren't being used like in the first example. In C, like in other programming languages, you can use statements that evaluate to true or false rather than using the boolean values true or false directly.

这里有一些重要的区别。 首先, stdbool.h未包含在内。 没关系,因为没有像第一个示例那样使用truefalse 。 与其他编程语言一样,在C语言中,您可以使用评估结果为true或false的语句,而不是直接使用布尔值truefalse

Also notice the condition in the parenthesis of the if statement: n == 3. This condition compares n and the number 3. == is the comparison operator, and is one of several comparison operations in C.

还要注意if语句括号中的ifn == 3 。 此条件将n与数字3进行比较。 ==是比较运算符,并且是C中几个比较运算之一。

if...else嵌套 (Nested if...else)

The if...else statement allows a choice to be made between two possibilities. But sometimes you need to choose between three or more possibilities.

if...else语句允许在两种可能性之间做出选择。 但是有时您需要在三种或更多可能性之间进行选择。

For example the sign function in mathematics returns -1 if the argument is less than zero, +1 if the argument is greater than zero, and returns zero if the argument is zero.

例如,数学中的符号函数如果参数小于零则返回-1,如果参数大于零则返回+1,如果参数小于零则返回零。

The following code implements this function:

以下代码实现了此功能:

if (x < 0)sign = -1;
elseif (x == 0)sign = 0;elsesign = 1;

As you can see, a second if...else statement is nested within else statement of the first if..else.

如您所见,第二条if...else语句嵌套在第一条if..else else语句内。

If x is less than 0, then sign is set to -1. However, if x is not less than 0, the second if...else statement is executed. There, if x is equal to 0, sign is also set to 0. But if x is greater than 0, sign is instead set to 1.

如果x小于0,则sign设置为-1。 但是,如果x不小于0,则执行第二条if...else语句。 在那里,如果x等于0,则sign也设置为0。但是,如果x大于0,则sign设置为1。

Rather than a nested if...else statement, beginners often use a string of if statements:

初学者通常使用一串if语句,而不是嵌套的if...else语句:

if (x < 0) {sign = -1;
}if (x == 0) {sign = 0;
}if (x > 0) {sign = 1;
}

While this works, it's not recommended since it's unclear that only one of the assignment statements (sign = ...) is meant to be executed depending on the value of x. It's also inefficient – every time the code runs, all three conditions are tested, even if one or two don't have to be.

尽管这可行,但不建议这样做,因为目前尚不清楚仅根据x的值执行赋值语句之一( sign = ... )。 这也是低效率的–每次运行代码时,都会测试所有三个条件,即使不必一个或两个条件也是如此。

else ... if语句 (else...if statements)

if...else statements are an alternative to a string of if statements. Consider the following:

if...else语句是if字符串的替代方案。 考虑以下:

#include <stdio.h>int main(void) {int n = 5;if(n == 5) {printf("n is equal to 5!\n");} else if (n > 5) {printf("n is greater than 5!\n");}return 0;
}

Output:

输出:

n is equal to 5!

If the condition for the if statement evaluates to false, the condition for the else...if statement is checked. If that condition evaluates to true, the code inside the else...if statement's curly braces is run.

如果if语句的条件评估为false,则检查else...if语句的条件。 如果该条件的值为真,则运行else...if语句大括号内的代码。

比较运算符 (Comparison Operators)

Operator name Usage Result
Equal To a == b True if a is equal to b, false otherwise
Not Equal To a != b True if a is not equal to b, false otherwise
Greater Than a > b True if a is greater than b, false otherwise
Greater Than or Equal To a >= b True if a is greater than or equal to b, false otherwise
Less Than a < b True if a is less than b, false otherwise
Less Than or Equal To a <= b True if a is less than or equal to b, false otherwise
操作员姓名 用法 结果
等于 a == b 如果a等于b ,则为true,否则为false
不等于 a != b 如果a不等于b true,否则为false
比...更棒 a > b 如果a大于b则为true,否则为false。
大于或等于 a >= b 如果a大于或等于b ,则为true;否则为false
少于 a < b 如果a小于b ,则为true;否则为false
小于或等于 a <= b 如果a小于或等于b ,则为true;否则为false

逻辑运算符 (Logical Operators)

We might want a bit of code to run if something is not true, or if two things are true. For that we have logical operators:

如果某件事不正确,或者如果有两件事是真实的,我们可能希望运行一些代码。 为此,我们有逻辑运算符:

Operator name Usage Result
Not (!) !(a == 3) True if a is not equal to 3
And (&&) a == 3 && b == 6 True if a is equal to 3 and b is equal to 6
Or (||) a == 2 || b == 4 True if a is equal to 2 or b is equal to 4
操作员姓名 用法 结果
不是( ! ) !(a == 3) 如果a 等于3,则为true
和( && ) a == 3 && b == 6 如果a等于3 并且 b等于6,则为true
或( || ) a == 2 || b == 4 如果a等于2 b等于4,则为true

For example:

例如:

#include <stdio.h>int main(void) {int n = 5;int m = 10;if(n > m || n == 15) {printf("Either n is greater than m, or n is equal to 15\n");} else if( n == 5 && m == 10 ) {printf("n is equal to 5 and m is equal to 10!\n");} else if ( !(n == 6)) {printf("It is not true that n is equal to 6!\n");}else if (n > 5) {printf("n is greater than 5!\n");}return 0;
}

Output:

输出:

n is equal to 5 and m is equal to 10!

有关C比较的重要说明 (An important note about C comparisons)

While we mentioned earlier that each comparison is checking if something is true or false, but that's only half true. C is very light and close to the hardware it's running on. With hardware it's easy to check if something is 0 or false, but anything else is much more difficult.

虽然我们前面提到过,每个比较都是在检查某件事是正确还是错误,但这仅是正确的一半。 C非常轻巧,靠近它所运行的硬件。 使用硬件,很容易检查是否为0或false,但其他任何事情都更加困难。

Instead it's much more accurate to say that the comparisons are really checking if something is 0 / false, or if it is any other value.

相反,更准确地说是比较实际上是检查某个值是否为0 / false,或者是否为其他任何值。

For example, his if statement is true and valid:

例如,他的if语句是正确和有效的:

if(12452) {printf("This is true!\n")
}

By design, 0 is false, and by convention, 1 is true. In fact, here’s a look at the stdbool.h library:

按照设计,0为假,按照惯例,1为真。 实际上,这里是看stdbool.h库的:

#define false   0
#define true    1

While there's a bit more to it, this is the core of how booleans work and how the library operates. These two lines instruct the compiler to replace the word false with 0, and true with 1.

尽管还有更多内容,但这是布尔值如何工作以及库如何运行的核心。 这两行指示编译器将单词false替换为0,将true替换为1。

翻译自: https://www.freecodecamp.org/news/if-statements-in-c/

c语言中的if语句

c语言中的if语句_If ... C中的其他语句解释相关推荐

  1. 转载——C语言中float,double类型,在内存中的结构(存储方式)

    最近在做一个数据格式分析和转换的项目,第一次接触底层的二进制代码存储,看的一头雾水,看到这个帖子后对于在Windows系统下数据的存储方式有了更多的了解,将原文分享一下: 原文地址为http://ww ...

  2. 在c语言中char型数据在内存中的储存形式为什么

    在c语言中char型数据在内存中的储存形式为"ASCII码".在C语言中,将一个字符常量放到一个字符变量中,实际并不是把该字符本身放到内存单元中,而是将与该字符相对应的ASCII码 ...

  3. 在c语言中 char型数据在内存中的存储形式是,在c语言中char型数据在内存中的存储形式是什么?...

    在c语言中char型数据在内存中的存储形式是ASCII码.在C语言中,char型数据是将一个字符常量放到一个字符变量中,并不是把该字符本身放到内存单元中去,而是将该字符的相应的ASCII代码放到存储单 ...

  4. c语言中point的用法_关于C语言Switch语句,先学这些技巧够不够?

    一.C语言中switch的用法 1.switch后面括弧内的[表达式],ANSI标准允许它为任何类型: 2.当表达式的值与某一个case后面的常量表达式的值相等时,就执行此case后面的语句,否则,就 ...

  5. r语言中的while循环_R编程中的While循环

    r语言中的while循环 In addition to the for loop we discussed earlier, R also offers another kind of loop to ...

  6. 编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  7. c语言中值程序,编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  8. c语言中time函数作用,C语言中时间的基本用法小结

    前言 在我学的这些编程语言中,总是记不住它们的时间处理方式,每次用到时都要重新看,所以想着在这里记录下来,也方便用到时查找,也方便有需要的朋友们参考. time_t和struct tm 在C语言中用t ...

  9. c语言中point的用法_C/C++中 *和amp;的爱恨情仇

    C++中&和*的用法一直是非常让人头疼的难点,课本博客上讲这些的知识点一般都是分开讲其用法的,没有详细的总结,导致我在这方面的知识结构格外混乱,在网上找到了一篇英文文章简单总结了这两个符号的一 ...

最新文章

  1. 手机和Linux蓝牙通信,[原创]linux下手机与蓝牙的连接配置
  2. 基于jsp+mysql+mybatis+Spring boot简单学生成绩信息管理系统
  3. Pandas Window对象
  4. BZOJ2209 [Jsoi2011]括号序列 splay
  5. C++基础——bitset与vectorbool
  6. 最新win10系统下载64位
  7. 发动机压缩比怎么计算公式_加几号油它说的算 解析发动机压缩比奥秘
  8. 没有公网ip怎么访问家里内网
  9. 【LeetCode-13】-罗马数字
  10. 目标检测入门实战:贪吃蛇小游戏
  11. 列表的join方法,类方法formkeys,删除,集合,深浅拷贝赋值,冒泡排序
  12. h标签本身自带间距 去除方法
  13. RFID读卡器增量更新思路与代码实现
  14. 去除chrome自动填充黄底样式
  15. 景深决定照相机什么特性_2017自学考试《摄影基础》备考练习题及答案
  16. SSL/TLS类安全漏洞及SLB安全漏洞问题
  17. Day18-恶魔低语: [递归迭代], 两种方法掌握一下
  18. 物理学家看人工智能:懂了你就不怕了
  19. C语言的输入输出模型
  20. 全职刷机工具PhoenixSuit的使用教程

热门文章

  1. Angular响应式表单及表单验证
  2. ES6 let和const 命令
  3. c和python区别_C语言和python的区别
  4. 1小时学会:最简单的iOS直播推流(四)如何使用GPUImage,如何美颜
  5. Flex 布局详解 - 转自阮一峰老师
  6. 03-NSPredicate谓词
  7. docker宿主机访问docker容器服务失败
  8. DSSM(DEEP STRUCTURED SEMANTIC MODELS)
  9. Listener监听器之HttpSessionListener
  10. 07.LoT.UI 前后台通用框架分解系列之——强大的文本编辑器