运算优先顺序/循环
在C编程语言中:
这个我不明白。是不是说(例如)如果语句中 += 在 -= 之后,则首先评估 += ?或者如果 * 在 - 之前,则 - 首先执行?我需要了解优先级。
有人能给我写两个或三个复杂的循环,其中包括:一些计数器变量、彼此之间的 3 或 4 或 5 个循环、数组、printf 和字符串之类的东西吗?如果我想在编程课程或明天的考试中取得好成绩,我需要学会在纸上手动完成循环。
这是 nit 作业,即没有什么可交的,只是为明天的 C 考试做准备。
In the C programming language:
This I do not understand. Is it saying that (for example) if += is after -= in a statement, the += is evaluated first? Or if * is before a -, the - is executed first? I need to understand precedence.
Can someone write me two or three complicated loops which include: a few counter variables, 3 or 4 or 5 loops within eachother, arrays, printf's and strings and stuff? I need to learn to manually go through loops on paper really well if I want to ace my programming course, or the exam I have tomorrow..
This is nit homework, i.e., nothing to hand in, just preparing for my exam in C tomorrow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
优先级图具有垂直分量(优先级)和水平分量(关联性)。
基本上,列表中较高位置的操作首先完成,因此
a + b * c
的计算结果为a + (b * c)
。请注意,这并不意味着b * c
在a
之前计算,只是*
操作在+ 之前完成
。实现可以首先计算a
,然后乘以b
和c
并将其添加到已经计算的<代码>a。对于简单的表达式,这没有什么区别,但如果表达式的其中一项除了提供简单值之外还具有副作用,那么它可能会咬伤您。我的意思是像
i++
这样的东西,它具有递增i
的副作用,或者调用修改全局变量的函数,或者将信息写入数据库。如果两个运算符具有相同的优先级,则结合性将起作用。这规定了具有相同优先级组的操作如何组合在一起。
因此
+
和-
(具有从左到右的关联性)意味着a + b - c
的计算结果为(a + b ) - c
.另一方面,
+=
和-=
具有从右到左的关联性,因此a += b -= c
的计算结果为 <代码>a += (b -= c)。就循环而言,您可以从以下内容开始:
我建议您快速尝试理解它,然后输入它进行编译和运行。
The precedence chart has a vertical component (precedence) and a horizontal component (associativity).
Basically, operations higher in the list are done first, so
a + b * c
is evaluated asa + (b * c)
. Note that this doesn't mean thatb * c
is calculated beforea
, just that the*
operation is done before the+
. An implementation is free to calculatea
first, then multiplyb
andc
and add that to the already calculateda
.For simple expressions, this makes no difference but it can bite you if one of the terms of your expression has a side-effect beyond supplying a simple value. By that, I means things like
i++
which has the side-effect of incrementingi
, or a call to a function which modifies global variables, or writes information to a database.Where two operators have the same precedence, associativity takes over. This dictates how operations with the same precedence group together.
So
+
and-
(which have associativity of left to right) means thata + b - c
evaluates as(a + b) - c
.On the other hand,
+=
and-=
have right-to-left associativity so thata += b -= c
evaluates asa += (b -= c)
.In terms of loops, you can start with the following:
I would suggest you have a quick try in understanding it, then type it in to compile and run.
运算符优先级和结合性不指定 C 计算表达式的顺序。它指定子表达式如何组合在一起。
+=
和-=
具有相同的优先级,并且从右到左关联。这意味着在此表达式中:C 将其分组为:
因此,从
b
中减去的值是c
,而与a
相加的值code> 是表达式b -= c
的结果(这是b
的新值)。未指定实际发生的顺序。试试这个嵌套循环 - 它在做什么?
Operator precedence and associativity does not specify the order in which C evaluates expressions. It specifies how subexpressions are grouped together.
+=
and-=
have equal precedence, and associate right-to-left. This means that in this expression:C groups it as:
So the value that is subtracted from
b
isc
, and the value that is added toa
is the result of the expressionb -= c
(which is the new value ofb
). The order in which this actually occurs is not specified.Try this nested loop out - what is it doing?
1) “图表中同一行上的运算符具有相同的优先级”,因此
a + b - c
的计算结果为(a + b) - c
。从上到下阅读该页面以获取操作顺序。因此,对于a + b + (c * ++d)
,顺序是 d 加 1,乘以 c,然后将整个数量添加到数量a + b< /代码>。
2) For 循环就像普通的书一样从上到下阅读。例如:(伪代码更正为正确的 C -zw)
您从 i 为 0 开始,j 为 0,i 和 j 保持 0,而 k 为 10, 5, 2, 1,然后返回,j 为 1,而i 保持 0,k 为 10, 5, 2, 1。重复直到 j 为 10,然后返回并使 i 为 1。重复整个过程,直到 i 为 100。内部循环对于正在进行的循环的每个值都运行完成。
1) "Operators on the same line in the chart have the same precedence," so
a + b - c
evaluates as(a + b) - c
. Read that page from top to bottom to get the order of operations. So fora + b + (c * ++d)
the order would be add 1 to d, multiply that by c, and then add that whole quantity to the quantitya + b
.2) For loops just read through like a normal book, top to bottom. For example: (pseudocode corrected to proper C -zw)
You start with i as 0, and j as 0, i and j stay 0 while k is 10, 5, 2, 1, then you go back up and j is 1 while i remains 0 and k is 10, 5, 2, 1. Repeat until j is 10, then go back up and make i 1. Repeat this whole thing until i is 100. An internal loop is run to completion for every value of the proceeding loop.