Why, using "and" in a for loop and "or" in a while loop gives the same result?

Question:

I am trying to practice writing these loops, and I had an exercise which asked me to print numbers from 0 to 20 that aren’t divisible by 3 or 5.
For the while loop, I wrote this code:

#solution with while

i = 0

while i < 21:
    i += 1
    if i % 3 == 0 or i % 5 == 0:
        continue
    print(i)

Whereas, for the for...in loop, I struggled because I found out that I needed to use and instead of or here.
The code is as follows:

#solution with for

for k in range(21):
    if k % 3 != 0 and k % 5 != 0:
        print(k)

Why did I have to change the logical operator?

In my head, the first rows of the two codes do the same thing, iterate a number from 0 to 20. So the condition, after these, should be equal for both the iterations used.
Can anyone explain to me what am I missing here?

Asked By: spool

||

Answers:

The two loop types are totally equivalent. You had to use and instead of or, and != instead of ==, because you reversed the logic.

In the first case, you are saying "if the number is divisible then skip it, else print it" while in the second one_ "if the number is not divisible then print (else do nothing)"_.

Answered By: gimix

Because you have also changed the conditions for the results in the while loop you have used ==, but in for loop you have used != if you modify the code you will get the same output by or operator in both

for k in range(21):
    if k % 3 == 0 or k % 5 == 0:
        continue
    print(k)
Answered By: Sujal

In while loop.

if i % 3 == 0 or i % 5 == 0: # this means when i is evenly divided by 3 or 5 then don't do anything. 
        continue
    print(i) # means only print i when i is not evenly divided by 3 and 5

And in the for loop

if k % 3 != 0 and k % 5 != 0: # here this means print i when k is not evenly divided by 3 and 5. Which same as while loop
        print(k)


You just reverse order and nothing. You can use the same method in both way and you will get the same result.

While loop

i = 0
while i < 21:
    i += 1
    if i % 3 != 0 and i % 5 != 0:
        print(i)

For Loop

for a in range(21):
    if a % 3 != 0 and a % 5 != 0:
        print(a)
Answered By: codester_09

This isn’t a loop type problem, the issue is in using different equality operators in your conditions – != instead of ==.

After changing or to and, the result stays the same because you’ve accidentally used De Morgan’s second law (negation of disjunction):
De Morgan's 2nd law


Now, let’s look at the code.

In the while loop:

  • the program doesn’t print i:
    • if it’s divisible by 3 (but not necessarily by 5)
      or
    • if it’s divisible by 5 (but not necessarily by 3).

In other words:

  • the program prints i:
    • if it’s not divisible by 3
      and
    • if it’s not divisible by 5.

Noe, let’s check, what says the condition in the for...in loop:

  • the program prints k:
    • if it’s not divisible by 3
      and
    • if it’s not divisible by 5.

Looks like the same condition, doesn’t it?
As you noticed, in the In other words: paragraph, we’ve naturally used the negation of disjunction.

To sum up, both programs for i / k lower than 21 print:

0
1
2
4
7
8
11
13
14
16
17
19

If you still aren’t sure, try to replace the conditions between loops:

i = 0
while i < 21:
    i += 1
    if i % 3 != 0 and i % 5 != 0:
        print(i)
for k in range(21):
    if k % 3 == 0 or k % 5 == 0:
        continue
    print(k)
Answered By: maciejwww
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.