我有一个程序,它使用for循环检查条件语句并对数据集运行测试,其中大多数是一个if
可能分支的线性语句.我的问题是带括号的for循环如何决定范围内外的内容?例如:
我的程序中的for循环:
for( i =0; i < (sizeof(exact_roots) / sizeof(bn_comlplex)); i++){
if(fabs(roots[k].re - exact_roots[i].re) < 0.00001 && fabs(roots[i].im - exact_roots[i].im) < 0.00001)
rootmatch++;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下需要括号?for循环会将第三行视为for循环的一部分还是丢弃它并给我一个编译错误?
如果没有括号的for循环的极端情况怎么样,循环如何处理呢?
for(i = 0; i < num; i++)
if(something)
....
else //is this still considered apart of the loop?
....
Run Code Online (Sandbox Code Playgroud)
for(i = 0; i < num; i++)
if(something)
....
else //is this still considered apart of the loop?
....
Run Code Online (Sandbox Code Playgroud)
是的,它仍然是循环的一部分.整个if () .... else ...
构造是一个陈述.
但使用牙套可以减轻您的担忧.使用大括号,可以立即清楚范围是什么.