如何在循环(作用域)的 if 语句中使用此变量
我正在使用 Arduino 进行编程,我相信它使用 C 作为基本语言。我有这个循环:
int i = 0;
void loop()
{
//Set The output pin to the MIDI note number - 60.
int pinNumber = noteNumber - 60;
if (midiMessage == 144) {
if (velocity == Triggered) {
registerWrite(pinNumber, HIGH);
i = pinNumber;
}
}
if (midiMessage == 128) {
if ((i % 8) == (pinNumber % 8)) {
if (velocity == Stopped) {
registerWrite(pinNumber, LOW);
}
}
}
}
我知道上面的代码不能正常工作,但我想做的是如果满足第一个条件 if (velocity ==已触发)
,然后在满足 midiMessage == 128
条件时就可以使用它。
I'm programming something with an Arduino, which I believe uses C as the base language. I have this loop:
int i = 0;
void loop()
{
//Set The output pin to the MIDI note number - 60.
int pinNumber = noteNumber - 60;
if (midiMessage == 144) {
if (velocity == Triggered) {
registerWrite(pinNumber, HIGH);
i = pinNumber;
}
}
if (midiMessage == 128) {
if ((i % 8) == (pinNumber % 8)) {
if (velocity == Stopped) {
registerWrite(pinNumber, LOW);
}
}
}
}
I know the above code doesn't work right, but what I'm trying to do is assign the i
variable if the first condition is met if (velocity == Triggered)
, and then be able to use it when the midiMessage == 128
condition is met.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
i
是一个全局变量,这意味着它在程序启动时被初始化为 0(显式),然后可以由任何可以看到它的代码进行更改。所以,如果你的问题是:
(根据您在问题中留下的注释,似乎是这样)那么答案是肯定的。
换句话说,当您输入
loop()
且midiMessage
等于 144 时,它会将i
设置为pinNumber
()。
noteNumber - 60 输入
loop()
且midiMessage
等于 128,它将使用当前的i
(无论它最后设置为什么)。在
loop()
中输入midiMessage
等于 128,然后在midiMessage
等于 144 时调用它,即我
。如果
i
的声明位于loop()
函数内,则每次该函数运行时都会重新初始化它 调用,但这里的情况并非如此。当然,良好的编程实践建议您应该将其放在函数中如果这是您唯一使用它的地方(情况可能并非如此)。
这将其“可见性”(a) 限制为该函数,以便其他代码不会无意中使用它(这对于名为
的变量来说尤其是一个问题i
- 你可能想为它想一个更好的名字)。要像这样限制其可见性,同时仍然允许它存在于函数调用之间,您可以使用类似的方法:
这仍然会在程序启动时初始化它一次(即使它位于函数内部),并在
循环()
。它还阻止该函数外部的代码“查看”该变量。(a) 请注意,此处使用的“可见性”不是 ISO 标准使用的术语。我在介绍性编码课程中使用这个术语,因为“可见性”比“范围”更容易理解。
i
is a global variable, meaning that it's initialised to 0 (explicitly) when your program starts, and then can be changed by any code that can see it.So, if your question is:
(and it seems to be, based on the comments you left in the question) then the answer is yes.
In other words, when you enter
loop()
withmidiMessage
equal to 144, it will seti
topinNumber
(noteNumber - 60
).Then the next time you enter
loop()
withmidiMessage
equal to 128, it will use the currenti
(whatever it was last set to).Of course, if you enter
loop()
withmidiMessage
equal to 128 before you call it whenmidiMessage
equals 144, the value ofi
will be zero.If that declaration of
i
was inside theloop()
function, then it would be re-initialised each time the function was called, but that's not the case here.Of course, good programming practice would suggest that you should put it inside the function if that's the only place you use it (this may not be the case).
This restricts its "visibility" (a) to that function so that there's no chance other code may use it inadvertently (this is especially a problem for a variable called
i
- you may want to think of a better name for it).To restrict its visibility like that while still allowing it to exist between function calls, you would use something like:
This still initialises it once, at program startup (even though it's inside the function) and keeps its value in between different invocations of
loop()
. It also stops code from outside of that function from "seeing" the variable.(a) Note that "visibility" as used here is not the term the ISO standard uses. I use that term for my introductory coding classes since "visibility" is easier understood than "scope".