C++:为什么在 if 语句中有一个 return?
C++ : why have a return in a if statement?
我想弄清楚为什么有人会在 Arduino 循环中编写以下代码段。对我来说,这没有意义,为什么在 if 语句中有一个 return?它是否只是 return 到循环的开始而不继续循环的其余部分。这是感兴趣的片段:
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 <value>;
或 return;
之后(对于 void 函数)程序退出循环,也退出函数。函数 returns <value>
(对于非空函数)。此语句适用于执行功能已经不是必需的。
能够 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
。
简单return; in void 函数用于“中断”函数。当你不想执行它的其余部分时,程序只会 return 在堆栈中运行所谓的执行函数。
我想弄清楚为什么有人会在 Arduino 循环中编写以下代码段。对我来说,这没有意义,为什么在 if 语句中有一个 return?它是否只是 return 到循环的开始而不继续循环的其余部分。这是感兴趣的片段:
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 <value>;
或 return;
之后(对于 void 函数)程序退出循环,也退出函数。函数 returns <value>
(对于非空函数)。此语句适用于执行功能已经不是必需的。
能够 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
。
简单return; in void 函数用于“中断”函数。当你不想执行它的其余部分时,程序只会 return 在堆栈中运行所谓的执行函数。