c++ - C++:为什么在 if 语句中有返回值?
问题描述
我试图弄清楚为什么有人会在 Arduino 循环中编写以下代码部分。对我来说,这没有意义,为什么在 if 语句中有返回?它是否只是返回到循环的开头而不继续循环的其余部分。这是感兴趣的片段:
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
这是完整的代码
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
解决方案
能够return
及早使用函数是使用函数的主要原因之一。抛开 Arduino 的特殊性不谈,一个非常常见的情况是例如跳出嵌套循环。假设你有
for (int i = 0; i < imax; ++i) {
for (int j = 0; j < jmax; ++j) {
do_something(i,j);
if (some_condition(i,j)) {
// now I want to break out of both loops...
}
}
}
break
只打破最里面的循环。如果您想跳出多个嵌套循环,您可以引入bool
标志并使其成为循环条件的一部分,尽管这会很快以一团糟而告终。通常更简洁的方法是将循环放在一个函数中,并且简单地return
从函数中:
void my_nested_loops() {
for (int i = 0; i < imax; ++i) {
for (int j = 0; j < jmax; ++j) {
do_something(i,j);
if (some_condition(i,j)) {
return; // breaks out of both loops
}
}
}
}
从某种意义上说,您的代码是这个的由内而外的变体。该loop
函数在循环中为您调用,并且由于该循环不在您的控制范围内,您不能使用它continue
来继续下一个循环迭代,但您可以调用return
.
推荐阅读
- json - 在 Springboot+Angular 中传递 Json 状态码
- r - 使用 R 下载 pdf 会生成损坏的文件
- java - 在 Spring Data JPA 中查询 NOTIN 的更好方法
- git - 修复合并分支时 Git 无法识别文本编辑器关闭事件的场景
- c# - 对所有依赖项使用自定义版本的程序集
- git - Gitlab runner 13.1.1 签出提交而不是我的分支
- javascript - JS Date 对象是如何工作的?
- vba - Mchart activex-为特定列设置特定颜色
- javascript - javascript动态选择/点击事件
- bash - bash- 使用 sed 删除字符串