Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110437

Re: Break and Continue: While Loops

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Break and Continue: While Loops
Date 2016-06-23 17:34 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <nkh6j9$acb$1@reader1.panix.com> (permalink)
References <639b00e0-7b9d-4ed4-96ad-6afbcd536786@googlegroups.com>

Show all headers | View raw


In <639b00e0-7b9d-4ed4-96ad-6afbcd536786@googlegroups.com> Elizabeth Weiss <cake240@gmail.com> writes:

> i=0
> while 1==1:
>    print(i)
>    i=i+1
>    if i>=5:
>      print("Breaking")
>      break

> Why is Breaking going to be printed if i only goes up to 4?

Your code prints i and THEN adds one to it.

So i is 4, it gets printed, then 1 is added to it, so it becomes 5
and then the loop exits.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-22 21:17 -0700
  Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-22 21:49 -0700
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:08 -0700
  Re: Break and Continue: While Loops Rustom Mody <rustompmody@gmail.com> - 2016-06-22 22:05 -0700
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
  Re: Break and Continue: While Loops DFS <nospam@dfs.com> - 2016-06-23 01:20 -0400
    Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:09 -0700
  Re: Break and Continue: While Loops Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-23 18:28 +1000
  Re: Break and Continue: While Loops alister <alister.ware@ntlworld.com> - 2016-06-23 09:53 +0000
  Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 11:15 +0100
    Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-23 21:39 +1000
      Re: Break and Continue: While Loops Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2016-06-23 11:57 +0000
        Re: Break and Continue: While Loops Lawrence D’Oliveiro <lawrencedo99@gmail.com> - 2016-06-26 00:32 -0700
          Re: Break and Continue: While Loops Steven D'Aprano <steve@pearwood.info> - 2016-06-26 22:18 +1000
      Re: Break and Continue: While Loops BartC <bc@freeuk.com> - 2016-06-23 16:52 +0100
        Re: Break and Continue: While Loops Chris Angelico <rosuav@gmail.com> - 2016-06-24 02:23 +1000
        Re: Break and Continue: While Loops pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-06-24 09:53 +0200
  Re: Break and Continue: While Loops John Gordon <gordon@panix.com> - 2016-06-23 17:34 +0000
  Re: Break and Continue: While Loops Elizabeth Weiss <cake240@gmail.com> - 2016-06-26 12:42 -0700
    Re: Break and Continue: While Loops Quivis <quivis@domain.invalid> - 2016-06-28 23:32 +0000

csiph-web