Break and Continue: While Loops
Elizabeth Weiss
cake240 at gmail.com
Sun Jun 26 15:09:16 EDT 2016
On Thursday, June 23, 2016 at 1:06:09 AM UTC-4, Rustom Mody wrote:
> On Thursday, June 23, 2016 at 9:47:23 AM UTC+5:30, Elizabeth Weiss wrote:
> > CODE #1:
> >
> > i=0
> > while 1==1:
> > print(i)
> > i=i+1
> > if i>=5:
> > print("Breaking")
> > break
> >
> > ------
> > I understand that i=0 and i will only be printed if 1=1
> > The results of this is
> > 0
> > 1
> > 2
> > 3
> > 4
> > Breaking
> >
> > Why is Breaking going to be printed if i only goes up to 4? It does say if i>=5? Shouldn't this mean that the results should be:
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> >
> > CODE #2:
> >
> > i=0
> > while True:
> > i=i+1
> > if i==2:
> > print("Skipping 2")
> > continue
> > if i==5:
> > print("Breaking")
> > break
> > print(i)
> >
> > ------
> >
> > Questions:
> > 1. what does the word True have to do with anything here?
> > 2. i=i+1- I never understand this. Why isn't it i=i+2?
> > 3. Do the results not include 2 of 5 because we wrote if i==2 and if i==5?
> > 4. How is i equal to 2 or 5 if i=0?
> >
> > Thanks for all of your help!
>
> I suggested in your other post (Subject While Loops)
> that the predecessor of python ABC's syntax for assignment would help unconfuse you
>
> ie the ASSIGNMENT x=y we write as PUT y IN x
>
> Using that rewrite your CODE 1 as
>
> PUT 0 in i
> while 1==1:
> print(i)
> PUT i+1 IN i
> if i>=5:
> print("Breaking")
> break
>
> Now try and rethink what that does
> Then repeat for your other examples that confuse
Got it! Thank you!
More information about the Python-list
mailing list