for loop

Hello, everyone!

I don't understand why p<a doesn't give a mistake. The initial values for both p and a are 2. And my second question is related to if(del). I tried to write if(del=true) but the result was different. What is the meaning of if(del)? I thought it checks is the value for del is true but then why if(del=true) works differently? Thanks a lot!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 for(int a=2; N>0; a++)
   {   bool del=true;
       for(int p=2; p<a; p++)
       {
           if(a%p==0)
           {
               del=false;
               break;
           }
       }
       if(del){
        cout << a << endl;
       N--;}
   }
Last edited on
I don't understand why p<a doesn't give a mistake. The initial values for both p and a are 2.
Why should it? They both integers, they can be compared, that comparsion is free to return false if p is not less than a. Actually compiler will probably optimise code, notice that condition is always false and throw out inner loop completely.

What is the meaning of if(del)?
Check if del variable is true.
why if(del=true) works differently?
Because what you wrote is "assign true to del and check if del is true". There is difference between == and =: first is comparsion, second is assigment.
Thanks a lot!
Topic archived. No new replies allowed.