谁让自己基础差呢,突然想到这个问题,写了个小测试例子:
#include <iostream>
using namespace std;
void test(int x);
int main() {
int a;
cout<<"Input a num"<<endl;
cin>>a;
cout<<a<<"Just input"<<endl;
test(a);
cout<<"OK"<<endl;
return 0;
}
void test(int x)
{
if(x>100){
cout<<"大于100"<<endl;
return;
}
else
{
cout<<"不大于100"<<endl;
return ;
}
cout<<"test"<<endl;
}
运行结果:
Input a num
150
150Just input
大于100
OK
程序没有打印“test”字符串,而打印了ok.
结论:if语句里的return,使程序跳出if所在的函数,返回到母函数中继续执行。
C++ if-return机制解析
本文通过一个简单的C++示例程序,展示了if语句中return的作用机制:当if语句块内的return被执行时,会立即退出当前函数并返回到调用该函数的位置继续执行后续代码。此特性对于理解函数调用流程至关重要。
1943

被折叠的 条评论
为什么被折叠?



