Logic to while and Output for function