return语句是否导致执行跳出当前执行的函数
Does the return statement cause the execution to jump out from the currently executed function?
bool accept3()
{
int tries = 1;
while (tries<4) {
cout << "Do you want to proceed (y or n)?n"; // write question
char answer = 0;
cin >> answer; // read answer
switch (answer) {
case 'y':
return true;
case 'n':
return false;
default:
cout << "Sorry, I don't understand that.n";
++tries; // increment
}
}
cout << "I'll take that for a no.n";
return false;
}
return语句是否导致执行跳出当前执行的函数(在本例中为accept3()),并且在返回后,它下面的代码将不运行,对吗?
return
语句不关心您是否处于循环的中间。当你return
某事时,你立即停止执行该函数
return
语句将导致执行跳出当前执行的函数(在本例中为accept3()
),因此函数内的循环也被跳出。
当存储在answer
中的输入为y
时,进入switch语句的第一个case,其中包含一个return true;
,它从函数accept3()
返回,从而也离开了while
-循环。
因为return
语句终止当前函数并将表达式的结果返回给调用者。While循环条件在本例中并不重要。
相关文章:
- 执行函数时导致崩溃的变量
- 在多个时间戳处执行函数
- 程序不会执行函数 c++
- 仅在函数模板中为那些定义了函数的类型执行函数
- 如何在 c++ 的类声明中执行函数?
- 从 std::vector 迭代器中执行函数指针
- 异步 60 秒延迟后C++执行函数?
- 如果发生错误,则返回错误值,否则,继续执行函数
- 执行函数两次
- C++11:如何在 n 秒后在主线程中执行函数
- C++执行函数和 lambda 内部
- 从主线程C 11执行函数调用的线程
- 现代C++执行函数返回指针的方法
- 为什么Win API线程执行函数,而标准线程则没有
- varargs宏以执行函数调用或将其转换为字符串
- 单步执行函数,但不进入VS2017的参数评估
- 加载 Lua 文件并使用变量而不执行函数
- 尽可能在编译时执行函数契约
- 在QT应用程序中创建并执行函数
- 如何使用boostsio_service.post()执行函数并使用数组或指针作为参数