gpt4 book ai didi

c - 在 C 中重新启动嵌套循环的理想方法?

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:52 25 4
gpt4 key购买 nike

我是微 Controller C 语言编程的新手,在使用嵌套循环时遇到了一些问题。以下是我的 C 代码中的示例结构。问题在后面提到

// I am trying to ramp down the while loop
while (i>= stop_value)
{
step_value = default_value;
R32(a particular register, content_register);
if (content_register = a_set_value)
{
if( step_value <= step_max)
{
step_value = step_value +1;
i=start_value;
continue;
}
if(step_value =step_max)
{
// do something;
break;
}
}
WR32(a particular register, content_reset_register); // resetting the register
i=i-1;
}

基本上,我正在尝试执行以下操作:降低循环直到它到达 stop_value 并在降低时读取特定的寄存器。如果寄存器有定义值,则检查 step_value<= step_max。如果 step_value < step_max 则使用 continue 语句重置 while 循环,并将 i 设置为初始值。这个时候我的step_value应该是最新的值而不是默认的step值。我明白上面的代码对于 step_value 并不完全正确,因为我将 step_value 初始化为 default_value。步骤值?)但是,如果 step_value 等于我的 step_max,那么只需做一些事情并随后中断(这部分有效!)。另外,当我使用 continue 语句时,我应该重置寄存器的内容。

所以,我想用最新的step_value有效地重置while循环,当我的step_value小于step_max时寄存器的内容重置?感谢您的回复!

最佳答案

在这段代码中:

   if( step_value <= step_max)
{
step_value = step_value +1;
i=start_value;
continue;
}
if(step_value =step_max)
{
// do something;
break;
}

第二个 if 永远无法到达。您正在检查 <=,然后使用 continue 跳到循环底部。

根据你上面的逻辑:

then check if the step_value<= step_max. If step_value < step_max then reset the while loop by using continue statement, and setting i to initial start value.

这段代码应该是:

   if( step_value < step_max)
{
step_value = step_value +1;
i=start_value;
continue;
}
...

然后可以评估第二个 if 。这应该会生成编译器警告,例如对无法访问的代码和 if 语句中的赋值感到疯狂。请打开您的编译器警告并听取它们!

关于c - 在 C 中重新启动嵌套循环的理想方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15905987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com