'else if statement is wrong how can i go to next statement in nested for loop
for(;;){ /********* infinite iteration**************/
for(k=1;k<=8;k++){
loop1:
for(n=0; n<48;n++){
for(i=0; i<8; i++){
for(j=0; j<natm; j++){
...................
statements;
}
}
}
E[iter] /*** Result of three loops such as n,i,j ***/
if (E[iter] < E[iter-1])
{
iter++;
print the value of E[iter];
/***again calculate the E[iter] ****/
goto loop1;
}
else if(E[iter]>E[iter-1])
{
/** stop the current for loop of k and move to k=2 **/
/*** here is the problem for me i want to get rid of this loop and goto to the next iteration for loop (k =2)***/
}
}
}
Solution 1:[1]
replace your comment /* here is the problem ... with
continue;
continue statement will skip the current iteration and proceed to the next one, without breaking the entire loop as oposed to break; statement.
Solution 2:[2]
Based on the @calccrypto comment:
If you want to go to the next iteration of the for loop:
else if(E[iter]>E[iter-1])
{
continue;
}
Solution 3:[3]
You can use the continue; command as above answers show.
Or try to apply the same logic you with the goto loop1; command, having it above the for loop for k
Solution 4:[4]
5 levels of for loop — that's quite an achievement! And a label too! I'm not going to try to guess what they do.
You need:
else if (E[iter] > E[iter-1])
{
/** stop the current for loop of k and move to k=2 */
/** here is the problem for me I want to get rid of this */
/** loop and goto to the next iteration for loop (k =2) */
k = 2;
goto loop1;
}
The loop1 label is just inside the for (k = 1; k <= 8; k++) loop; jumping to it with the value of k set to 2 will go to the iteration with k == 2.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Freeman Lambda |
| Solution 2 | |
| Solution 3 | user1873900 |
| Solution 4 | Jonathan Leffler |
