Nested loops

Hello can someone help me understand the first if statement, i know what % does but both i and j are the same number all the time? or am i wrong?

1
2
3
4
5
6
7
8
    int i, j;
   
   for(i=2; i<100; i++) {
      for(j=2; j <= (i/j); j++)
        if(!(i%j)) break; // 
        if(j > (i/j)) cout << i << " is prime\n";

   // "if(!(i%j)) break; " It is this line i don't understand.  
For others reading this, I was wrong, it was meant to be formatted that way...
This post is bad don't read it.

First, it looks like you want braces around your inner for loop.

Second, realize that i/j truncates the answer to an integer (edit: Although that's not really the problem here), plua this doesn't make much sense to begin with, seeing as you're trying to check for primes.
Look at what will happen on the first run through:
* i will be 2
* j will be 2
* for loops then checks if 2 <= (2/2)
* this is false, so it won't even execute from the start.

If you initialized your for loop as for (int i = 2; i < 100; i++) and likewise for the j-loop, it would prevent you from accidentally using "i", or "j" when you didn't mean to. (If you're working in C though, your compiler might not like this.)


This part is still good
___________________

// "if(!(i%j)) break; " It is this line i don't understand.

This works because 0 evaluates to false, and 1 evaluates to true, and !0 == 1.
Ex:
i = 4,
j = 2,
it will check 4 mod 2, which is 0. Then it negates the 0, making it a 1, and this 1 is seen as "true" to the if statement.

Others may disagree but I find this notation really annoying to work with,
it would be much easier to read as if (i%j == 0) (In English, if i is divisible by j)
Last edited on
If(!(i%j)) means that if(i%j == 0) .
If this is TRUE, then j is a factor of i hence i is not prime.
Since i is not prime by that condition, there is no need to check for any othere values of j. That is the reason for the break
for(j=2; j <= (i/j); j++), how can j be 2 if it should be less or equal to i/j or 2/2 = 1?
I'm not sure what you're asking, you assign 2 to j in the first statement. 2 is not less than or equal to i/j (2/2), therefore it ends.

If you're checking for primality for numbers 2 to 99, the naive approach would just be to iterate through each number, check for divisibility (return false if it is able to evenly divide), until i, (i/j) doesn't make sense.

I would rather set up your problem like this:
1
2
3
4
5
6
7
int main()
{
    for (int i = 2; i < 100; i++)
    {
        checkPrimality(i);
    }
}

This way you separate your nested loop into a function by itself, easier to focus on without worrying about nested fors.
Last edited on
Yeah it looks easier but i haven't worked with functions yet hehe, the first one is also a quite complicated example given to a beginner in my opinion (i'm following a tutorial).

but is j = 2 all the time? is it only "i" that becomes greater?

so it divides j(2) with i all the way up to 100 and if there is 0 modulus it breaks the loop? or am i still wrong?
Last edited on
Okay I think I just confused you more, very sorry, from the start I just assumed that you had meant to put a bracket around the for loop, so that's what I was looking at, and incorrectly assumed some things.

Your code actually works fine, but I disagree with the author's use of using j outside of its respective loop... but that's beside the point

If you want to check the values at each iteration, do something like

1
2
3
4
5
6
7
8
9
10
11
    for(i=2; i<100; i++)
    {
        std::cout << i << std::endl;   
        for(j=2; j <= (i/j); j++)
        {
             std::cout << j << std::endl;
             if (i%j == 0) break;
             //if ...
        }
        //...
    }
that should let you see what's happening more clearly. J does get greater than 2 if the number is odd, it checks each number below it if that number is divisible. It breaks the loop when it's evenly divisible because you know that value of i is not prime then.
Last edited on
What i still don't understand is when does 'j' go up because both the for loops has the i++ and j++, so i think it must be the same number all the time? i mean in run 2 'i' will be 3 i guess? then 'j' must be 3 too? so 3/3 = 1.
I don't know what "j <= (i/j)" are for.
Last edited on
Topic archived. No new replies allowed.