我正在为明天的累积考试而学习,在上一次考试中我错了以下问题.我希望有人可以向我解释这个问题? (〜m)是什么意思?
I am studying for cumulative exam I have tomorrow and I got the following question wrong on a previous exam. I was hoping someone could explain this question to me? What does (~m) mean?
问题说: 执行以下脚本后,m的值是多少?
The question says: After executing the following script, what is the value of m?
a=1; b=2; m=0; if (~m) m = m+1; if (a-b > 0) m = m+1; else m = m -1; end elseif (m > 1) m = m + 2; else m = m - 2; end正确答案是0,但是为什么呢?我猜想m = -2
The correct answer is 0, but why? I would have guessed that m = -2
推荐答案~表示NOT.但是,除非数值等于0,否则所有数值都被视为TRUE.
The ~ means NOT. However, numeric values are all considered TRUE unless they are identically equal to 0.
因此,此逻辑实际执行的命令是:
So, the commands which are actually executed by this logic are:
m = m+1; %Following if (~m) m = m-1; $Following else此外,代码中还有一个嵌套的if语句.如果您使用了多个级别的缩进,将更易于阅读.
Also, there is a nested if statement in the code. It will be easier to read if you used multiple level indentations.
更多推荐
Matlab运算子
发布评论