r/AskProgramming • • Oct 11 '26

For loop

How would I store 3 different answers to the same 3 questions asked using a for loop. I know how to store one answer, but not all three while still using the for loop

0 Upvotes

13 comments sorted by

View all comments

Show parent comment

2

u/JaguarMammoth6231 Oct 11 '26 edited Oct 11 '26

Good point. A lot of people start off thinking that if you're going to do something similar multiple times you should use a loop, but it's really best when you're doing exactly the same thing. 

You don't want to follow the for-if or for-case anti-pattern of 

for i = 1 to 3
  if i = 1, ask question 1
  if i = 2, ask question 2
  if i = 3, ask question 3

1

u/balefrost Oct 11 '26

Yes, though when people say "use a loop", they usually mean "loop over an array of data, and do the same thing every time" (i.e. ask the question at that index in the array).