如何跳出for循环
continu只能跳出当前for循环继续for循环,但是我想跳出for之外的for循环如何做呢?
下面的代码我用了goto , 想跳到loop 处的for循环处继续循环,但是不对,不知道怎么做了
谢谢大家解答!!!
for(int i=0;i
{
loop:for(int j=0;jgcNc[0].cellNc[i].NombreFrequences;j++)
{
int freq = rand() % m +1;
//exist || non satisfait Cii contrainte(CSC). exemple rand() 7,encore rand() 7
if(a[freq] == 1)
{
j--;
continue;
}
//non exist && satisfait contrainte. exemple: rand() 7, apres rand() 8
if(a[freq] != 1)
{
for(int k=0;kgcNc[0].cellNc[i].NombreFrequences;k++)
{
if((p->gcNc[0].cellNc[i].frequences[k]-freq >= Cii || p->gcNc[0].cellNc[i].frequences[k]-freq <= -Cii ))
{
a[freq] = 1;
p->gcNc[0].cellNc[i].frequences[j] = freq;
printf("%d ",p->gcNc[0].cellNc[i].frequences[j]);
}else{
goto loop; ///?????????????????????????????????
printf("continue");
}
}
}
}
------解决方案--------------------
表示用goto的都太nb了,目前不敢用。提供个其它方法。可以设一个标志位,跳出一层循环后立马判断标志位是否需要跳出,需要跳出break;
int judge = 0;
for()
{
for()
{
if(...)
{
judge = 1
continue;
}
}
if(1 == judge)
{
break;
}
}
------解决方案--------------------
for (int i=0;i
LOOP2:
for (int j=0;jgcNc[0].cellNc[i].NombreFrequences;j++) {
int freq = rand() % m +1;
//exist
------解决方案--------------------
non satisfait Cii contrainte(CSC). exemple rand() 7,encore rand() 7
if (a[freq] == 1) {
j--;
continue;
}
//non exist && satisfait contrainte. exemple: rand() 7, apres rand() 8
if (a[freq] != 1) {
for (int k=0;kgcNc[0].cellNc[i].NombreFrequences;k++) {
int v=p->gcNc[0].cellNc[i].frequences[k]-freq;
if ( v <= -Cii
------解决方案--------------------
Cii <= v) {
a[freq] = 1;
p->gcNc[0].cellNc[i].frequences[j] = freq;
printf("%d ",p->gcNc[0].cellNc[i].frequences[j]);
} else {
i++;//需要加这句才能开始i的下次循环
goto LOOP2;
}
}
}
}
}
for (A;B;C) D;
//等价于
{
A;
while (1) {
if (!(B)) break;
D;
C;
}
}
------解决方案--------------------
你是想继续执行这个loop: for(int j=0;jgcNc[0].cellNc[i].NombreFrequences;j++),
int judge = 0;
for(int i=0;i
{
for(int j=0;jgcNc[0].cellNc[i].NombreFrequences;j++)
{
int freq = rand() % m +1;
//exist
------解决方案--------------------
non satisfait Cii contrainte(CSC). exemple rand() 7,encore rand() 7
if(a[freq] == 1)
{
j--;
continue;
}
//non exist && satisfait contrainte. exemple: rand() 7, apres rand() 8
if(a[freq] != 1)
{
for(int k=0;kgcNc[0].cellNc[i].NombreFrequences;k++)