switch lambda function to while statement