【发布时间】:2024-01-23 23:30:01
【问题描述】:
我试图有效地推断出哪些条件导致程序忽略了 if 语句,而不使用一系列 if 语句来单独验证每个变量的相对完整性。
这可能吗?
bool state = false;
int x = 0;
int y = 1;
int z = 3;
if(x == 0 && y == 1 && z == 2) {
// Do something...
state == true;
}
if(state == false) {
std::cout << "I did not execute the if statement because the following
conditions were not met: " << std::endl;
/*Find a way to make the program output that z != 3 stopped the
conditional from running without directly using if(z != 2)*/
}
【问题讨论】:
-
如果你想知道到底是哪一个失败了,那你为什么不分别做每个条件呢?
-
由于您只想检查变量值,因此请使用调试器在所需位置停止程序执行并在其帮助下打印每个变量。
-
打印else部分的所有值。我认为那会做。在运行时,您可以知道哪个值是意外的。
标签: c++ logical-operators