Python for Beginners; Lesson 22 (loops) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python for Beginners; Lesson 22 (loops)

I'm trying to understand/envision this better. If this is a loop, then wouldn't X=10 come again after x=-1? And therefore X=-1 changed back to X=10 ? What part is the loop if not all 4 lines? What am I missing in this question?

7th Feb 2022, 5:51 AM
Mohammed PressTV.ir Fan
Mohammed PressTV.ir Fan - avatar
5 Answers
+ 1
G'day Mohammed PressTV.ir Fan did you learn a bit more about loops? They have a few things that happen: They have a initialisation, often starting the counter at 0. Eg i=0 They have an end condition, often when the counter doesn't exceed a certain value. Eg i<=max They have an increment, often it is advancing the counter by 1. Eg i++ (same as writing i=i+1). There are also options to leave out some of those things.....
8th Feb 2022, 7:38 AM
HungryTradie
HungryTradie - avatar
+ 1
Peace. Yes, thank you also for your answer. Great.
8th Feb 2022, 7:45 AM
Mohammed PressTV.ir Fan
Mohammed PressTV.ir Fan - avatar
+ 1
The for loop can have all three things, eg for (i=0;i<=42;i++){ //your code within the loop } The while loop often only has the end condition. Eg while (tradie==0){ //your code to run until the end condition is not true The do while loop always runs once before checking the end condition. Eg do{ //your code within the loop }while (hungry==1)
8th Feb 2022, 7:45 AM
HungryTradie
HungryTradie - avatar
+ 1
Another loop type is recursion of a function. This one seems much more complicated than it is, but because it is scary most courses leave it until near the end. I don't have a good example of that one....
8th Feb 2022, 7:51 AM
HungryTradie
HungryTradie - avatar
+ 1
Thank you so much.
8th Feb 2022, 7:52 AM
Mohammed PressTV.ir Fan
Mohammed PressTV.ir Fan - avatar