如何在循环(作用域)的 if 语句中使用此变量

发布于 2024-12-08 20:32:24 字数 660 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-12-15 20:32:24

i 是一个全局变量,这意味着它在程序启动时被初始化为 0(显式),然后可以由任何可以看到它的代码进行更改。

所以,如果你的问题是:

它会在对 loop() 函数的不同调用之间保持其值吗?”

(根据您在问题中留下的注释,似乎是这样)那么答案是肯定的。

换句话说,当您输入 loop()midiMessage 等于 144 时,它会将 i 设置为 pinNumber ()。

noteNumber - 60 输入 loop()midiMessage 等于 128,它将使用当前的 i(无论它最后设置为什么)

。在 loop() 中输入 midiMessage 等于 128,然后在 midiMessage 等于 144 时调用它,即


如果 i 的声明位于 loop() 函数内,则每次该函数运行时都会重新初始化它 调用,但这里的情况并非如此。

当然,良好的编程实践建议您应该将其放在函数中如果这是您唯一使用它的地方(情况可能并非如此)。

这将其“可见性”(a) 限制为该函数,以便其他代码不会无意中使用它(这对于名为 的变量来说尤其是一个问题i - 你可能想为它想一个更好的名字)。

要像这样限制其可见性,同时仍然允许它存在于函数调用之间,您可以使用类似的方法:

void loop (void) {
    static int persistentVar = 0;
    :
    :
}

这仍然会在程序启动时初始化它一次(即使它位于函数内部),并在 循环()。它还阻止该函数外部的代码“查看”该变量。


(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:

Will it maintain its value between different calls to the loop() function?"

(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() with midiMessage equal to 144, it will set i to pinNumber (noteNumber - 60).

Then the next time you enter loop() with midiMessage equal to 128, it will use the current i (whatever it was last set to).

Of course, if you enter loop() with midiMessage equal to 128 before you call it when midiMessage equals 144, the value of i will be zero.


If that declaration of i was inside the loop() 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:

void loop (void) {
    static int persistentVar = 0;
    :
    :
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文