当前位置: 首页 > 编程日记 > 正文

c语言中的if语句_If ... C中的其他语句解释

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 nameUsageResult
Equal Toa == bTrue if a is equal to b, false otherwise
Not Equal Toa != bTrue if a is not equal to b, false otherwise
Greater Thana > bTrue if a is greater than b, false otherwise
Greater Than or Equal Toa >= bTrue if a is greater than or equal to b, false otherwise
Less Thana < bTrue if a is less than b, false otherwise
Less Than or Equal Toa <= bTrue 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 nameUsageResult
Not (!)!(a == 3)True if a is not equal to 3
And (&&)a == 3 && b == 6True if a is equal to 3 and b is equal to 6
Or (||)a == 2 || b == 4True 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语句

相关文章:

设计模式之笔记--装饰模式(Decorator)

装饰模式&#xff08;Decorator&#xff09; 定义 装饰模式&#xff08;Decorator&#xff09;&#xff0c;动态地给一个对象添加一些额外的职责&#xff0c;就增加功能来说&#xff0c;装饰模式比生成子类更为灵活。 类图 描述 Component&#xff1a;被装饰者和装饰者共有的基…

整型数组负数放左面,其他放右面,要求时空复杂度:O(n), O(1)。

例如&#xff1a;处理前&#xff1a;{5 -3 6 -7 -6 1 8 -4 0 0}&#xff0c;处理后&#xff1a;{-3 -7 -6 -4 5 6 1 8 0 0}. #include <iostream> #include <algorithm> using namespace std;const int LEN 10; void printInt(int c){cout<<c<<"…

[bzoj] 1176 Mokia || CDQ分治

原题 给出WW的矩阵&#xff08;S没有用&#xff0c;题目有误&#xff09;&#xff0c;给出无限次操作&#xff0c;每次操作的含义为&#xff1a; 输入1:你需要把(x,y)(第x行第y列)的格子权值增加a 输入2:你需要求出以左下角为(x1,y1),右上角为(x2,y2)的矩阵内所有格子的权值和,…

sql子查询示例_SQL更新查询示例说明

sql子查询示例In this article, were going to learn how to use the SQL update statement - what it is, what it can do, and what you need to be aware of before using it.在本文中&#xff0c;我们将学习如何使用SQL更新语句-它是什么&#xff0c;它可以做什么以及在使用…

keepalived+nginx安装

安装keepalivednginx做为公司服务器前端高可用反向代理安装nginx 1、yum install -y pcre pcre-devel gcc-c zlib zlib-devel openssl openssl-devel 2、cd /usr/local/soft 3、wget http://nginx.org/download/nginx-1.12.2.tar.gz 4、tar -zxvf nginx-1.12.2.tar.gz 5、cd ng…

Nexus Repository Manager 3.0 发布

著名仓库管理工具Nexus&#xff0c;在2016年4月6日发布3.0版本&#xff08;包括OSS版&#xff09;&#xff0c;相较2.*版本有很大的改变&#xff1a; 1. 从底层重构&#xff0c;从而提高性能&#xff0c;增强扩展能力&#xff0c;并改善用户体验 2. 升级界面&#xff0c;增加更…

计算整型数的二进制中包含多少个1

方法很多啊&#xff0c;比如&#xff1a;//1.靠循环&#xff1a; int calculate(unsigned int n){int count 0;unsigned int mark 0x1;for(int i 0; i < 32; i){if(n&mark){count;mark<<1;}}return count; }//2. 据说不用循环就能算出来的牛叉方法。木有测试。…

nvm npm不是内部命令_npm作弊表-最常见的命令和nvm

nvm npm不是内部命令npm or the Node Package Manager, is one of the most used tools for any Node.js developer. Heres a list of the most common commands youll use when working with npm.npm或Node Package Manager是Node.js开发人员最常用的工具之一。 这是使用npm时…

快速排序的实现与注意点

先上实现了的C代码&#xff1a; 1 #include <ctime>2 #include <cstdio>3 #include <cstdlib>4 #include <iostream>5 using namespace std;6 const int maxn 100;7 int a[maxn], n;8 void quick_sort(int left, int right) {9 if(left > …

iOS 线程之GCD的高级使用方法

之前的一篇关于线程的blog已经为大家介绍了GCD的简单使用方式及样例说明&#xff0c;今天因为项目中有特殊的应用GCD的实例&#xff0c;为大家介绍两种特殊需求的使用GCD的方法。 目的&#xff1a;实现一件事情做完&#xff0c;再做下一件事情。确保函数的运行周期。 解决方式…

构造次优查找树

似乎有些错误&#xff0c;但是错在哪了呢&#xff1f; #include <iostream> #include <cmath> using namespace std;const int NUM 9;int value[NUM] {1,2,3,4,5,6,7,8,9}; float weight[NUM] {1,1,2,5,3,4,4,3,5}; float sum_weight[NUM]; void init_sum_weigh…

同步等待 异步等待_异步/等待和承诺的解释

同步等待 异步等待The async / await operators make it easier to implement many async Promises. They also allow engineers to write clearer, more succinct, testable code.async / await 运算符使实现许多异步Promises变得更加容易。 它们还允许工程师编写更清晰&#…

使用 GDB 调试多进程程序

使用 GDB 调试多进程程序 来源 https://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/index.html GDB 是 linux 系统上常用的 c/c 调试工具&#xff0c;功能十分强大。对于较为复杂的系统&#xff0c;比如多进程系统&#xff0c;如何使用 GDB 调试呢&#xff1f;考虑下面这…

理解Lucene索引与搜索过程中的核心类

理解索引过程中的核心类 执行简单索引的时候需要用的类有&#xff1a; IndexWriter、Directory、Analyzer、Document、Field 1、IndexWriter IndexWriter&#xff08;写索引&#xff09;是索引过程的核心组件&#xff0c;这个类负责创建新的索引&#xff0c;或者打开已有的索引…

lua的table+setfenv+setmetatable陷阱

--file1.lua x funciton() print("this is x") end ------------- --file2.lua local t {} local _G _G setfenv(1,t) --设置了这个之后&#xff0c;只要是在本文件中对未声明变量的访问&#xff0c;全部会导致递归。 _G.setmetatable(t, { __index fu…

rest api_REST API

rest api历史 (History) REST stands for Representational State Transfer protocol. Roy Fielding defined REST in his PhD dissertation in 2000.REST代表再表象小号泰特贸易交接协议。 Roy Fielding在2000年的博士学位论文中定义了REST。 什么是REST API&#xff1f; (Wh…

0414复利计算6.0--结对

结对同伴&#xff1a;姓名&#xff1a;柯晓君学号&#xff1a;201406114210博客园地址&#xff1a;http://www.cnblogs.com/950525kxj/一、项目简介 开发工具&#xff1a;eclipse 开发语言&#xff1a;java 主要功能&#xff1a;复利单利的计算、贷款的计算以及投资运算三大功能…

把简单做到极致

我真的还没有认真想过我已经是一名即将毕业的大三学生了。关于自己的过去&#xff0c;关于自己的未来。 有时候也有想过好好反思一下自己的过去&#xff0c;却发现自己的过去总是被太多的无奈与遗憾填满。有时候想畅想一下自己的未来&#xff0c;却发现未来总是充满了未知与迷茫…

作为程序员,要取得非凡成就需要记住的15件事。

作为程序员&#xff0c;要取得非凡成就需要记住的15件事。1、走一条不一样的路在有利于自己的市场中竞争&#xff0c;如果你满足于“泯然众人矣”&#xff0c;那恐怕就得跟那些低工资国家的程序员们同场竞技了。2、了解自己的公司以我在医院、咨询公司、物流企业以及大技术公司…

craigslist_Craigslist,Wikipedia和丰富经济

craigslistYou’ve heard it before. Maybe you’ve even said it. “There’s no such thing as a free lunch.”你以前听过 也许你甚至已经说过了。 “没有免费的午餐之类的东西。” “You can’t get something for nothing.”“你不能一无所获。” “Somebody has to pay…

EXCEL基础篇(二)

本章主要内容 一、单元格操作 二、插入批注 三、自动求和 四、填充序列 五、查找、替换 六、对齐方式 七、定位 八、插入形状及设置形状 九、页面设置 一单元格操作 1、插入 a、插入单元格 一个单元格选中状态---右击插入&#xff08;单元左右移&#xff09;--即可 b、插入单…

lua5.2调用c函数成功的例子

1. main.c-----------------//动态库#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef _cplusplusextern "C"{#endif#include <lua.h>#include <lauxlib.h>#include <lualib.h>static void checktoptype(lua_St…

【转】Android Activity原理以及其子类描述,androidactivity

Android Activity原理以及其子类描述&#xff0c;androidactivity 简介 Activity是Android应用程序组件&#xff0c;实现一个用户交互窗口&#xff0c;我们可以实现布局填充屏幕&#xff0c;也可以实现悬浮窗口。一个app由很多个Actvitiy组合而成&#xff0c;它们之间用intent-…

python 文件追加写入_Python写入文件–解释了打开,读取,追加和其他文件处理功能

python 文件追加写入欢迎 (Welcome) Hi! If you want to learn how to work with files in Python, then this article is for you. Working with files is an important skill that every Python developer should learn, so lets get started.嗨&#xff01; 如果您想学习如何…

带有中文的字符串各个字符的获取c++程序

简单易懂&#xff0c;上代码&#xff1a; #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <vector> using namespace std;class CStr{char *c;typedef struct {int start;bool isChinese;} counter;int…

C#时间格式化(Datetime)用法详解

Datetime.ToString(String, IFormatProvider) 参数format格式详细用法&#xff1a; 格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间&#xff08;长日期和短时间&#xff09;FFullDateTimePattern&#xff08;长日期和长时间&#xff09;g常规&#xf…

python添加数组元素_Python列表附录–如何向数组添加元素,并附带示例说明

python添加数组元素欢迎 (Welcome) Hi! If you want to learn how to use the append() method, then this article is for you. This is a powerful list method that you will definitely use in your Python projects.嗨&#xff01; 如果您想学习如何使用append()方法&…

学习进度条--第七周

第七周 所花时间&#xff08;包括上课时间&#xff09; 10小时&#xff08;包括上课2小时&#xff09; 代码量&#xff08;行&#xff09; 152 博客量&#xff08;篇&#xff09; 2篇&#xff08;包括团队博客&#xff09; 了解到的知识点 对组内开发的软件进行讨论&am…

Mybatis获取插入记录的自增长ID

转自&#xff1a;http://blog.csdn.net/tolcf/article/details/39035259 1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”&#xff0c;其中keyProperty是Java对象的属性名&#xff0c;而不是表格的字段名。 <insert id"insert" parameter…

android中一种不支持的lua操作

今天写了一段lua代码&#xff0c;在win32中正常运行&#xff0c;在android中运行无效。 大概是这样的&#xff1a; ------file1.lua----- local t {} t.str "this is file1.t" return t ---------------------- -----file2.lua------ local t require &quo…