What is a Counter Variable in for Loops? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is a Counter Variable in for Loops?

This is the code in the lesson: words = ["hello", "world", "spam", "eggs"] counter = 0 max_index = len(words) - 1 while counter <= max_index:    word = words[counter]    print(word + "!")    counter = counter + 1  Here I am trying to understand it: while 0 is less than or equal to 3: word equals the list labeled words[0] print(word plus "!") counter = 1 I am not understanding what the counter does

21st Feb 2020, 8:11 PM
Oksana
3 Answers
+ 1
Okay thank you, I think I get it
21st Feb 2020, 9:43 PM
Oksana
0
Counter is variable you used to count only certain numbers iterations to be done only... In this example, counter incremented every time one so for value 0 to 3(max_index), loop runs when it is 4,loop stop iterating.. It just user defined variable taken as a counter, so you can use any other variable like i, j, k...
21st Feb 2020, 8:28 PM
Jayakrishna 🇮🇳
0
Wel come..
21st Feb 2020, 9:58 PM
Jayakrishna 🇮🇳