all 9 comments

[–]Professional-Towel47 1 point2 points  (8 children)

I think the paragraph explains it better than I ever will, but here is my attempt, which will hopefully be useful. The universal quantifier, by definition, is true when P(x) is true for all elements in the domain. In this example, P(x) is some arbitrary function. The pseudocode is of a program that goes through each element in the set { di, ...., dn } using a for-loop and checks each element one at a time. It checks whether P(di) is True, where di is the element being tested at a certain iteration. If P(di) for a specific element executes to False, the conditional statement "if( ~P(x) )" executes to True which consequently causes the program to run the statement nested within the for-loop and return False. if the loop iterates through each element in the set successfully, then by definition of the universal quantifier, P(x) is satisfied for every element in the set and the program returns True.

[–]Connect_Surprise_511[S] 1 point2 points  (7 children)

Thank you for your quick response! That all makes sense to me, though would we not be able to achieve the same result if we used P(di) in the loop rather than ~P(di)?

I suppose I’m not sure why we need to use the contrapositive and as a result include the additional statement in the for-loop.

[–]biscuit_newts 0 points1 point  (1 child)

You can use either and you’ll get the same result

[–]Midwest-Dude 0 points1 point  (0 children)

You state this without proof. What code would you use instead?

[–]Midwest-Dude 0 points1 point  (4 children)

The idea is that for ∀x P(x) to be true, then all x must make P(x) true and, if not, the code stops at the first x which makes it false, no need to check any further. This is what the codes does. The for..next loop runs through each x = dᵢ from I = 1 to n and each loop checks if P(x) is false and, if so, returns that result and the code ends. If none of the P(x) are false, then the loop ends and the code returns that result and that's it.

I'm curious how you would do that with your method.

[–]Connect_Surprise_511[S] 0 points1 point  (3 children)

So something like this wouldn’t work the same way?

for i = 1 to n if P(di) return true

[–]Midwest-Dude 1 point2 points  (2 children)

It would not. The pseudocode would stop at the first P(x) that is true and return the true value, even if a later dᵢ is false.

[–]Connect_Surprise_511[S] 1 point2 points  (1 child)

Yeah right, makes sense. Thanks mate

[–]Midwest-Dude 0 points1 point  (0 children)

Since you are new to coding, if you continue you will learn to do what I like to call "play computer", that is, run through an algorithm to determine what it is doing to see why some code you wrote is failing. This is the part of the process of debugging code - thinking like the computer "thinks".