OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

i'm getting a problem with "while loops" in python (i'm a beginner)

  • Thread starter Thread starter mikey
  • Start date Start date
M

mikey

Guest
Code:
x=0
while x<5:
       print(x)
       if x==2:
          break
       x+=1

Code:
x=0
while x<5:
     if x==2:
         break
     print(x)
     x+=1

these are the two codes i runned, in the 1st one I am getting answer: 0 1 2 and in 2nd one I am getting answer:0 1

why is it so, what is the reason that I am getting different results, is there any specific logic?
<pre><code>x=0
while x<5:
print(x)
if x==2:
break
x+=1
</code></pre>
<pre><code>x=0
while x<5:
if x==2:
break
print(x)
x+=1
</code></pre>
<p>these are the two codes i runned, in the 1st one I am getting answer: 0
1
2
and in 2nd one I am getting answer:0
1</p>
<p>why is it so, what is the reason that I am getting different results, is there any specific logic?</p>
 
Top