Please explain the below code where I am going wrong that entire loop is not getting printed