Why is this code not working for this logic?

Why is this code not working for this logic?

Question is: -