嵌套循环逻辑中的 If 语句
If statement in a nested loop logic
我正在为一些逻辑问题苦苦挣扎。我有一个嵌套的 for 循环,里面有一个 if 语句。
foreach(object in list)
foreach(otherObject in otherList)
if(object.name == otherObject.name)
foo();
break;
else
bar();
每当 if 语句为假时,这将执行 bar() 。仅当 foo() 从未完成时,我如何才能执行 bar() ?抱歉,如果这是一个重复的问题,它可能是...
你可以只使用一个标志:
did_foo = false;
foreach(object in list)
foreach(otherObject in otherList)
if(object.name == otherObject.name)
foo();
did_foo = true;
break;
if (!did_foo)
bar();
我正在为一些逻辑问题苦苦挣扎。我有一个嵌套的 for 循环,里面有一个 if 语句。
foreach(object in list)
foreach(otherObject in otherList)
if(object.name == otherObject.name)
foo();
break;
else
bar();
每当 if 语句为假时,这将执行 bar() 。仅当 foo() 从未完成时,我如何才能执行 bar() ?抱歉,如果这是一个重复的问题,它可能是...
你可以只使用一个标志:
did_foo = false;
foreach(object in list)
foreach(otherObject in otherList)
if(object.name == otherObject.name)
foo();
did_foo = true;
break;
if (!did_foo)
bar();