Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

The break Keyword #

Sumit Rawal answered on May 16, 2023 Popularity 3/10 Helpfulness 1/10

Contents


More Related Answers

  • css word break
  • word-break
  • word break break word
  • break
  • word-break
  • break and cotinue
  • loop break easy
  • break out of loop

  • The break Keyword #

    0

    Sometimes, we need to exit the loop before it reaches the end. This can happen if we have found what we were looking for and don’t need to make any more computations in the loop.

    A perfect example is the one we have just covered. At a certain point, n1 is 23 and n2 is 27. Our condition of n1 + n2 == n has been fulfilled. But the loops keep running and comparing all other pairs as well. This is why the pair is printed twice. It would be nice to just stop it when the pair is found once.

    That’s what the break keyword is for. It can break the loop whenever we want.

    Let’s add it to the example above:

    Popularity 3/10 Helpfulness 1/10 Language python
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 16 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.