c - C 中的 while 循环中的 if

标签 c if-statement conditional-statements

我有以下代码:

while(tmp->next != NULL)
{
    if(tmp->code == (unsigned int)16777221)
    {
        CU_ASSERT_STRING_EQUAL("3GPP Zh", tmp->name);
    }
    if(strcmp((const char*)tmp->name, (const char*)"IUT-T Rs") == 0)
    {
        CU_ASSERT_EQUAL((unsigned int)16777235, tmp->code);
    }
    tmp = tmp->next;
}

我想做的是:一旦执行了if结构中的代码(也就是说,if条件被评估为true),我不想执行它在接下来的所有 while 循环中,我该怎么做?

最佳答案

int once = 0;

while(tmp->next != NULL)
{
    if(!once && tmp->code == (unsigned int)16777221)
    {
        once = 1;
        CU_ASSERT_STRING_EQUAL("3GPP Zh", tmp->name);
    }
    /* deal with subsequent if's in the same manner */
    tmp = tmp->next;
}

关于c - C 中的 while 循环中的 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263016/

相关文章:

c - 将 c 文件的输出放入 bash 变量中

c - 为什么 gcc 可以自动将符号标记为弱

Java if条件代码优化

c++ - 我正在编写一个非常简单的字母评分系统,if 语句是否最有效?

scheme - 一个 Racket 内的多个程序有条件吗?

C++数组评估

c - 如何捕获c中execvp()函数的输出?

java - 从 Java 使用命令行参数调用 C 程序

java - 没有 else 的 if 语句(当它看起来需要一个时)

java - 循环计算字符串中给定字符数的问题