'Nested loops Using continue pattern programs [closed]
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
{
continue;
}
printf(“\n%d %d”,i,j);
}
}
}
This is a nested loop program using continue statement Please explain Logic behind this code. ..
Solution 1:[1]
I hope adding the comments here will make it more explicit. I will also rearrange the code so it looks a bit better.
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
if (i == j) {
// When i equals to j, do nothing in the inner loop.
// i.e. jumps to the second line again.
continue;
}
// Otherwise (i != j) print the value of i and j.
printf(ā\n%d %dā,i,j);
}
}
The use of continue here may not be necessary and the above code is equivalent to
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
if (i != j) {
// print the value of i and j.
printf(ā\n%d %dā,i,j);
}
}
}
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 | Teddy van Jerry |
