试图理解这一行Groovy代码:
return strat?.descriptor?.displayName ?: "null"
Run Code Online (Sandbox Code Playgroud)
是?:if/else的简写吗?这是否意味着如果strat?.descriptor?.displayName不是null,打印它,或打印null?
我很困惑,因为没有在任何之间?和:像我通常会期望在一个if/else语句.
所以我有以下代码:
char command;
cin >> command;
if ( command == 'P' ) {
do_something();
}
if ( command == 'Q' ) {
cout << "Exit\n";
exit(0);
}
else {
cout << "command= " command << endl; //use for debugging
cout << "Non-valid input\n";
exit(1);
}
cout << "exit at completion\n";
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
当我使用输入时P,我的输出do_something()完成后是:
"output from do_something() function"
command= P
Non-valid input
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我仍然在第一个if语句中调用Non-valid inputafter do_something()?AKA为什么在do_something()完成时仍然会运行?