# I don't understand the conditions in nested loops .I am stuck

#### ahmarcaan1233

Joined Mar 8, 2020
8
I am stuck with this program. I don't understand the conditions in nested loops.
Another thing if number is 2, j<=(¡/j) because j=2. 2 divide by 2 is not greater or equal to j then condition is false then what would be output.pleae also elaborate your answer.!
I'm really stuck with it.
Code:
#include <stdio.h>
int main ()
{ /* local variable definition */     int i, j;
for(i=2; i<100; i++)
{         for(j=2; j <= (i/j); j++)
Moderators note : split into lines and used code tags

Last edited by a moderator:

#### bertus

Joined Apr 5, 2008
21,056
Hello,

The posted programs is not complete.
There are two opening curly braces , but no closing.
Can you post the complete program.

Bertus

#### ahmarcaan1233

Joined Mar 8, 2020
8
#include <stdio.h>

int main ()
{ /* local variable definition */
int i, j;

for(i=2; i<100; i++)
{
for(j=2; j <= (i/j); j++)
if(!(i%j))
break; // if factor found, not prime
if(j > (i/j))
printf("%d is prime\n", i);
}

#### ahmarcaan1233

Joined Mar 8, 2020
8
I don't understand the condition in loops.Just clear my concept

#### NeeQoo

Joined Oct 16, 2020
1
I think you should run your program with the debugger enabled and watch output for each step to understand your code behavior.

#### dl324

Joined Mar 30, 2015
12,269
I don't understand the condition in loops.Just clear my concept

Do you not understand what the conditional expressions do? Or do you mean that you don't know how nested loops work?

#### ahmarcaan1233

Joined Mar 8, 2020
8
I don't understand the condition with in loops for prime number.How this condition work

#### djsfantasi

Joined Apr 11, 2010
7,249
I don't understand the condition with in loops for prime number.How this condition work
Do you not understand conditions within the loops, like i%j?

Or do you not understand conditions as a parameter in the for statement, like j<=(i /j)?

I think you mean the latter. For each repetition of the loop, the current values of i and j are used to calculate i/j. If the value of j is less than or equal to that result, the loop stops.

#### Djsarakar

Joined Jul 26, 2020
352
Do you understand what's happen in nested loop

C:
#include<stdio.h>

int main(void)
{

int i = 0;
int j = 0;

for ( i = 0; i < 5; i++)   // Outer loop
{
printf(" i = %d \n", i);
for ( j = 0; j < 5; j++) // inner loop
printf(" j = %d \n", j);
}

return 0;
}
Output of program
Code:
i = 0
j = 0
j = 1
j = 2
j = 3
j = 4
i = 1
j = 0
j = 1
j = 2
j = 3
j = 4
i = 2
j = 0
j = 1
j = 2
j = 3
j = 4
i = 3
j = 0
j = 1
j = 2
j = 3
j = 4
i = 4
j = 0
j = 1
j = 2
j = 3
j = 4

#### dl324

Joined Mar 30, 2015
12,269
I don't understand the condition in loops.Just clear my concept
Even after you're told how to use code tags, you refuse:
Code:
#include <stdio.h>
int main () {
int i, j;
for (i = 2; i < 100; i++) {
for (j = 2; j <= (i / j); j++)
if (!(i % j))
break;          // if factor found, not prime
if (j > (i / j))
printf ("%d is prime\n", i);
}
}
Is this school work?

#### WBahn

Joined Mar 31, 2012
26,398
Where did this code come from? It looks like this is some kind of assignment where either you are given some code and asked to determine how it works, or you are given a problem and asked to develop some code to solve it. How we interact with you depends on which it is. But in either case you need to show your best efforts to do what the assignment calls for and we will try to help you identify and correct specific points on which you are going astray. We won't do your work for you as that will rob you of the learning opportunities that the assignment is intended to provide.

For starters, do you understand the problem that the program is trying to solve and the basic strategy that is being employed? Could you describe, to a sixth grader who knows nothing about computer programming, how to apply this strategy in such a way that they understand it and could do it with pencil and paper?