Jump to content

Loop execution process (Python vs C)


Raktim
 Share

Recommended Posts

Sir, I wrote the same code on Python and C. But the same code give two different result...my C code is...

for (i=0;i<10;i++)
  continue;
printf ("%d",i);

and the my python code is...

for i in range (0,10):
    continue
print (i)

The c code give 10 and python give 9 as result. So, what is loop execution process...what is the difference between loop execution process between Python and C?

Link to comment
Share on other sites

18 hours ago, justsomeguy said:

Range just returns a list of numbers from min to (max - 1).  Or, 0 through 9.  So, i is each number from 0 through 9.  You're not telling it to keep adding 1 to i, you're just telling it to loop for each number from 0 through 9.

okay, So, i make a little change to my python code...

for i in range (0,10):
   print(i)
print ("value of i after exit from loop",i)

The loop will execute until the condition became FALSE. So, the loop will execute from 0 to (10-1) mean 0 to 9 and condition become FALSE when i become 10...(10<10-->False)...am i right? Let me to show that what i thinking...

First it print,

Loop Body (value of i)        Increment (i=i+1)        Condition (i<10)              Status (T/F)

---------------------------------------------------------------------------------------------------------------------------------------

              0                                           1                                    1<10                              TRUE

              1                                           2                                    2<10                              TRUE   

              2                                           3                                    3<10                              TRUE  

              3                                           4                                    4<10                              TRUE

              4                                           5                                    5<10                               TRUE

              5                                           6                                    6<10                               TRUE

              6                                           7                                     7<10                               TRUE

              7                                           8                                     8<10                               TRUE

              8                                           9                                     9<10                               TRUE

              9                                           10                                   10<10                             FALSE  

So, the last value of i became 10 but when i print it show 9...Why? The process is also follows by C, am i right? But in the case of C it shows the result is 10...Why?   

 

Edited by Raktim
Link to comment
Share on other sites

19 hours ago, justsomeguy said:

Range just returns a list of numbers from min to (max - 1).  Or, 0 through 9.  So, i is each number from 0 through 9.  You're not telling it to keep adding 1 to i, you're just telling it to loop for each number from 0 through 9.

Sir, Range() concept is somewhat difference than  For Loop in C? or Same?

Link to comment
Share on other sites

The loop will execute until the condition became FALSE. So, the loop will execute from 0 to (10-1) mean 0 to 9 and condition become FALSE when i become 10...(10<10-->False)...am i right?

No, you're not right.  There is no condition, and there is no increment.  It is looping through members of a set.  In this case, the set is the set of integers from 0 through 9.  Maybe it will help you understand if you look at it in array syntax instead of using the range function:

for i in [0,1,2,3,4,5,6,7,8,9]

Looking at that, why would i ever be 10?  Look at another example:

for i in ["A", 3, 9, "Cat", 4, 100]

Would you look at that, and say that after the loop i is going to be 101?  Because that doesn't make sense.  i is not going to be set to a value that is not in the set.  The only thing that range does is return an array of numbers within a range.  There is no condition in the loop, and there is no increment (other than changing an internal array pointer to point to the next element in the array).

Link to comment
Share on other sites

11 hours ago, justsomeguy said:

 

 

No, you're not right.  There is no condition, and there is no increment.  It is looping through members of a set.  In this case, the set is the set of integers from 0 through 9.  Maybe it will help you understand if you look at it in array syntax instead of using the range function:

 


for i in [0,1,2,3,4,5,6,7,8,9]

 

Looking at that, why would i ever be 10?  Look at another example:

 


for i in ["A", 3, 9, "Cat", 4, 100]

 

Would you look at that, and say that after the loop i is going to be 101?  Because that doesn't make sense.  i is not going to be set to a value that is not in the set.  The only thing that range does is return an array of numbers within a range.  There is no condition in the loop, and there is no increment (other than changing an internal array pointer to point to the next element in the array).

Okay sir , thanks for your helping hand

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...