How to use *while loops to avoid unnecessary repetition when coding for questions🔗

Here is an example of how to use a *while loops to avoid unnecessary repetition when coding for questions by drawing from a pool of questions. You might use this configuration if you want people to be able to opt out after any single question. Including an option like this might substantially reduce the probability that someone finishes all the questions, but you might still want to include this option in certain circumstances (e.g., if there was a particularly distressing series of questions). In this loop, questions are presented to the participant as long as there are still questions left to ask and as long as they haven't clicked "No" in response to the question "Are you happy to continue?". You'll see that this includes an *if statement nested within the *while loop.

>> questionSet = ["First question", "Second question", "Third question", "Fourth question", "Fifth question"]
>> answers = {}
>> i = 1

*while: (not stopping) and (i <= questionSet.size)
	>> question = questionSet[i]

	*question: {question}
		*save: answer

	>> answers[question] = answer
	>> i = i + 1

	*if: i <= questionSet.size
		*question: Are you happy to continue?
				>> stopping = "Yes"

In this example, note that the answer to each question is saved in an association (>> answers[question] = answer). However, this might create extra work when it comes to processing the CSV later as all answers will be stored within the same collection in a single column. If you use collections like this in your study, you may wish to use the text-to-columns function (using commas as the separators) to split the collection into separate columns after you've loaded your CSV.