Problems with if clauses and loop

Thank you all, it actually works after switching that lines.