Python Forum
Confused by 'break' in the official documents
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confused by 'break' in the official documents
#1
Found these examples in the documentation.Was looking up For/Else loops. They both use a break statement. Was a bit confused why the break statement was there so ran them both without the break statements and the output is the same! So why have them?

Example 1.
for n in range(2, 10):
    for x in range(2, n):
        if n % x == 0:
            print(n, 'equals', x, '*', n/x)
            break
Example 2 - which is adding an else statement to the above (which I get)

for n in range(2, 10):
     for x in range(2, n):
         if n % x == 0:
             print(n, 'equals', x, '*', n//x)
             break
     else:
         # loop fell through without finding a factor
         print(n, 'is a prime number')
Reply


Messages In This Thread
Confused by 'break' in the official documents - by Chuck_Norwich - Apr-12-2020, 06:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  String int confused janeik 7 1,017 Aug-02-2023, 01:26 AM
Last Post: deanhystad
  Why Pip is not listed in the official Global Python Module index? quazirfan 2 731 Apr-21-2023, 10:55 AM
Last Post: snippsat
  Word documents merging crewdk 1 813 Apr-03-2023, 06:32 AM
Last Post: buran
  I am confused with the key and value thing james1019 3 912 Feb-22-2023, 10:43 PM
Last Post: deanhystad
  Pandas confused DPaul 6 2,469 Sep-19-2021, 06:45 AM
Last Post: DPaul
  is and '==' i'm confused hshivaraj 6 2,626 Sep-15-2021, 09:45 AM
Last Post: snippsat
  Сombine (Merge) word documents using python-docx Lancellot 1 11,361 May-12-2021, 11:07 AM
Last Post: toothedsword
  Confused with 'flags' tester_V 10 4,791 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Copy documents to Teams using python SallySmith 0 2,339 Mar-23-2021, 04:27 AM
Last Post: SallySmith
Question Mouseover(Hover/Float) text in PDF documents ak52 1 2,517 Feb-24-2021, 06:13 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020