return false, return true

Hi, I was trying to do an exercise but couldn't figure it out so had to find the solution online, the solution though contains a part that is slightly confusing to me. Hoping someone could maybe talk me through the section of code. The part that is causing me the issue is the is_prime function, mainly the return false, return true part.

First of all, because there is an if statement, i thought it was an "if else" statement with just the {} removed, but once i put the {} in to test, it works completely differently, so I take it that it doesn't function like an if else? ie - if (n%prime[p] == 0) return false, else return true.

Secondly I haven't came across the return true,false much yet, but I thought once one of them was reached the function would end and return back to, in this case, the main function to run the rest. But after it reaches true it will go back into the loop, and only exit the function once the loop is finished.

I am also confused about why there is no {} for the for loop, and when i put them in, it doesn't function like it should.

I think it may be the way it is written that is confusing, but like i said, when i put in the {}, which I am used to, the program doesn't function correctly.

I've followed how it flows using the debugger but unfortunately, its not helping me solve my issues.

If anyone could maybe help with any of my issues with understanding this, it would be greatly appreciated :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  bool is_prime(int n)
{
	for (int p = 0; p<prime.size(); ++p)
		if (n%prime[p] == 0)
			return false;	
	return true;	
}
int main()
try
{
	prime.push_back(2);

	for (int i = 3; i <= 100; ++i)	
		if (is_prime(i)) prime.push_back(i);	

	cout << "Primes: ";
	for (int p = 0; p<prime.size(); ++p)
		cout << prime[p] << '\n';

	keep_window_open("~");	
}
catch (runtime_error e) {	
	cout << e.what() << '\n';
	keep_window_open("~");	
}
catch (...) {	
	cout << "exiting\n";
	keep_window_open("~");	
}
Since we can't actually see your experiments with adding "{}", we can only guess a bit, but there's only so many permutations, so consider this:

1
2
3
4
5
6
7
8
9
10
11
12
bool is_prime(int n)
{
	for (int p = 0; p<prime.size(); ++p)
          {
		if (n%prime[p] == 0)
                    {
			return false;	
                    }
          }

	return true;	
}


I've exaggerated the indentation to make this obvious.

These "{}" enclosures represent how the compiler sees the code you posted. The "return false" happens when any divisor (through %) shows it is not a prime value, and is associated only with the test "if (n%prim.....".

As such, this is not an "if/else" clause, as you discovered. This is because the "%" test is inside the loop.

More generally, if any divisor shows no remainder, the test ends with false and returns (there can be none for it to be a prime).

Only when all options are tested, which is to say only when the entire "for" loop exhausts all possibilities, is a "true" to be returned.

So, if you arranged any enclosures which viewed the "if..return false" clause as if it were the first portion with an "else" clause trailing it, it was logically incorrect and would change the function's result.

The part you missed was the fact the loop fully enclosed the "%" test, so the return "true" was not part of that.

This is an example of where writing out braces even when they're not really required (because the enclosures only have one statement in them) is still functional to illustrate more clearly what the code actually says.

Last edited on
Thank you, you helped a lot. For some reason when I was following how the code was flowing using the debugger it was showing it hitting the return statement then going back into the loop. Now when I check it with your code I understand that it is not going to the return statement until it is ready to exit the function. Thanks again, have been trying to figure this out since yesterday xD
Topic archived. No new replies allowed.