Why conditional logic is not working properly?