I dnt understand the for loops please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I dnt understand the for loops please help

18th Jan 2017, 5:23 AM
steve
steve - avatar
2 Answers
0
for loop lets you repeat a set of instructions for a number of times that you can define in the for(arguments). in the example below for(int x=0; x<3; x++){cout<<x} the for loop in the above example runs every time x is less than 3 then increases x by one. the for loop will output the value x 3 times. the first time x = 0 and the foor loop runs x increases by 1 (x++) the second time x=1 and the for loop runs x increases by 1 (x++) the third time x=2 and the for loop runs x increases by 1 (x++) now x=3 and the for loop doesn't run anymore because the condition x<3 is not met. EDIT: my bad I didn't notice the question was "python" tagged. more or less same principles apply.
18th Jan 2017, 6:41 AM
seamiki
seamiki - avatar
0
for <x> in <collection> (do something with x) x = whatever you want the name of the variable to be, I tend to use item as the name and if number just x collection = array of values or range of numbers basically for runs through the collections values and on every round signes x as the value in that position on the collection. hope this helps
18th Jan 2017, 6:48 AM
Jani Sinkkonen
Jani Sinkkonen - avatar