How does a var inside While Loop intercept Print function and manipulate outside var?












0















I'm new to Python but having a hard time to understand the following While Loop code as it behaves very differently. I know this code work but I do not know HOW IT WORK. Top Python experts also have no idea.



x = 1
while x < 10:
print x
x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?




I do not know whether this related to control flow or global var. Please help me out to understand deeper.










share|improve this question

























  • It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

    – Carcigenicate
    Nov 13 '18 at 18:11













  • Is this a question about scope?

    – Matt Cremeens
    Nov 13 '18 at 18:12











  • You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

    – Adam Mitchell
    Nov 13 '18 at 18:12













  • There is only 1 variable named x in your script.

    – Johnny Mopp
    Nov 13 '18 at 18:13






  • 2





    Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

    – Kirk Broadhurst
    Nov 13 '18 at 19:25
















0















I'm new to Python but having a hard time to understand the following While Loop code as it behaves very differently. I know this code work but I do not know HOW IT WORK. Top Python experts also have no idea.



x = 1
while x < 10:
print x
x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?




I do not know whether this related to control flow or global var. Please help me out to understand deeper.










share|improve this question

























  • It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

    – Carcigenicate
    Nov 13 '18 at 18:11













  • Is this a question about scope?

    – Matt Cremeens
    Nov 13 '18 at 18:12











  • You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

    – Adam Mitchell
    Nov 13 '18 at 18:12













  • There is only 1 variable named x in your script.

    – Johnny Mopp
    Nov 13 '18 at 18:13






  • 2





    Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

    – Kirk Broadhurst
    Nov 13 '18 at 19:25














0












0








0


1






I'm new to Python but having a hard time to understand the following While Loop code as it behaves very differently. I know this code work but I do not know HOW IT WORK. Top Python experts also have no idea.



x = 1
while x < 10:
print x
x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?




I do not know whether this related to control flow or global var. Please help me out to understand deeper.










share|improve this question
















I'm new to Python but having a hard time to understand the following While Loop code as it behaves very differently. I know this code work but I do not know HOW IT WORK. Top Python experts also have no idea.



x = 1
while x < 10:
print x
x = x + 1 ## How does this VAR manipulate Print as the VAR comes late in the code?




I do not know whether this related to control flow or global var. Please help me out to understand deeper.







python python-2.7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:19









user2722968

2,67211637




2,67211637










asked Nov 13 '18 at 18:09









GashexGashex

76




76













  • It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

    – Carcigenicate
    Nov 13 '18 at 18:11













  • Is this a question about scope?

    – Matt Cremeens
    Nov 13 '18 at 18:12











  • You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

    – Adam Mitchell
    Nov 13 '18 at 18:12













  • There is only 1 variable named x in your script.

    – Johnny Mopp
    Nov 13 '18 at 18:13






  • 2





    Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

    – Kirk Broadhurst
    Nov 13 '18 at 19:25



















  • It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

    – Carcigenicate
    Nov 13 '18 at 18:11













  • Is this a question about scope?

    – Matt Cremeens
    Nov 13 '18 at 18:12











  • You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

    – Adam Mitchell
    Nov 13 '18 at 18:12













  • There is only 1 variable named x in your script.

    – Johnny Mopp
    Nov 13 '18 at 18:13






  • 2





    Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

    – Kirk Broadhurst
    Nov 13 '18 at 19:25

















It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

– Carcigenicate
Nov 13 '18 at 18:11







It's unclear what you're asking. What are you confused about? I think this is a question of declaration and I initialization vs reassignment.

– Carcigenicate
Nov 13 '18 at 18:11















Is this a question about scope?

– Matt Cremeens
Nov 13 '18 at 18:12





Is this a question about scope?

– Matt Cremeens
Nov 13 '18 at 18:12













You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

– Adam Mitchell
Nov 13 '18 at 18:12







You're increasing the value of x by 1 in each iteration of the loop. When x=10 you'll break out of the loop. When you exit the loop: x=10.

– Adam Mitchell
Nov 13 '18 at 18:12















There is only 1 variable named x in your script.

– Johnny Mopp
Nov 13 '18 at 18:13





There is only 1 variable named x in your script.

– Johnny Mopp
Nov 13 '18 at 18:13




2




2





Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

– Kirk Broadhurst
Nov 13 '18 at 19:25





Top Python experts also have no idea. This seems highly unlikely! Beware of learning from people who don't really know what they are doing.

– Kirk Broadhurst
Nov 13 '18 at 19:25












3 Answers
3






active

oldest

votes


















0














I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.



There are only two rules here that we need to follow:





  1. x must be declared before print so that print can access that piece of memory


  2. print must be in the same or inner scope to access 'x'


Example 1 - valid (because we follow rules 1 and 2)



x = 1
print x


Example 2 - valid (because we follow rules 1 and 2)



x = 1
while x < 10:
print x
x = x + 1


Example 3 - valid (because we follow rules 1 and 2)



x = 1
while x < 10:
print x
x = x + 1
print x


Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)



print x    
x = 1


Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)



y = 1
while y < 5:
x = 1
y = y + 1
print x


It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.






share|improve this answer


























  • What bout this one? x = 1 while x < 10: x = x + 1 print x

    – Gashex
    Nov 13 '18 at 19:00













  • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

    – Gashex
    Nov 13 '18 at 19:19











  • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

    – Gashex
    Nov 13 '18 at 19:22











  • i can't understand your example because in python tabbing is important

    – Max
    Nov 16 '18 at 7:02











  • check [link] (pastebin.com/mUXXdDsw)

    – Gashex
    Nov 16 '18 at 18:19



















0














x = 1 #Declares the variable
while x < 10: #checks whether the value of x is less than 10 every time the loop runs
print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)
x = x + 1 #this is the update part

Let me tell you one more thing, you do not have any case of global variable here.
If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link






share|improve this answer































    -1














    Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.



    This little software helped me to understand behind scenes http://pythontutor.com/visualize.html



    Conclusion:



    It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important






    share|improve this answer


























    • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

      – Thierry Lathuille
      Nov 17 '18 at 22:40











    • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

      – Gashex
      Nov 18 '18 at 5:04











    • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

      – Gashex
      Nov 18 '18 at 5:07











    • @ThierryLathuille Are you there?

      – Gashex
      Dec 1 '18 at 21:38











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287106%2fhow-does-a-var-inside-while-loop-intercept-print-function-and-manipulate-outside%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.



    There are only two rules here that we need to follow:





    1. x must be declared before print so that print can access that piece of memory


    2. print must be in the same or inner scope to access 'x'


    Example 1 - valid (because we follow rules 1 and 2)



    x = 1
    print x


    Example 2 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1


    Example 3 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1
    print x


    Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)



    print x    
    x = 1


    Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)



    y = 1
    while y < 5:
    x = 1
    y = y + 1
    print x


    It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.






    share|improve this answer


























    • What bout this one? x = 1 while x < 10: x = x + 1 print x

      – Gashex
      Nov 13 '18 at 19:00













    • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

      – Gashex
      Nov 13 '18 at 19:19











    • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

      – Gashex
      Nov 13 '18 at 19:22











    • i can't understand your example because in python tabbing is important

      – Max
      Nov 16 '18 at 7:02











    • check [link] (pastebin.com/mUXXdDsw)

      – Gashex
      Nov 16 '18 at 18:19
















    0














    I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.



    There are only two rules here that we need to follow:





    1. x must be declared before print so that print can access that piece of memory


    2. print must be in the same or inner scope to access 'x'


    Example 1 - valid (because we follow rules 1 and 2)



    x = 1
    print x


    Example 2 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1


    Example 3 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1
    print x


    Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)



    print x    
    x = 1


    Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)



    y = 1
    while y < 5:
    x = 1
    y = y + 1
    print x


    It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.






    share|improve this answer


























    • What bout this one? x = 1 while x < 10: x = x + 1 print x

      – Gashex
      Nov 13 '18 at 19:00













    • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

      – Gashex
      Nov 13 '18 at 19:19











    • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

      – Gashex
      Nov 13 '18 at 19:22











    • i can't understand your example because in python tabbing is important

      – Max
      Nov 16 '18 at 7:02











    • check [link] (pastebin.com/mUXXdDsw)

      – Gashex
      Nov 16 '18 at 18:19














    0












    0








    0







    I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.



    There are only two rules here that we need to follow:





    1. x must be declared before print so that print can access that piece of memory


    2. print must be in the same or inner scope to access 'x'


    Example 1 - valid (because we follow rules 1 and 2)



    x = 1
    print x


    Example 2 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1


    Example 3 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1
    print x


    Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)



    print x    
    x = 1


    Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)



    y = 1
    while y < 5:
    x = 1
    y = y + 1
    print x


    It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.






    share|improve this answer















    I believe your question is about scope. In the above example, a new memory address for var x is created on the first line and any code on lines 2,3,4 that access x can read/write to that variable.



    There are only two rules here that we need to follow:





    1. x must be declared before print so that print can access that piece of memory


    2. print must be in the same or inner scope to access 'x'


    Example 1 - valid (because we follow rules 1 and 2)



    x = 1
    print x


    Example 2 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1


    Example 3 - valid (because we follow rules 1 and 2)



    x = 1
    while x < 10:
    print x
    x = x + 1
    print x


    Example 4 - NOT valid (because we don't follow rule 1 and print is accessing a variable that has not yet been created in memory)



    print x    
    x = 1


    Example 5 - NOT valid (because we don't follow rule 2 and print doesn't have access to our variable's scope, x no longer exists in memory)



    y = 1
    while y < 5:
    x = 1
    y = y + 1
    print x


    It would do you good to read this entire section on variable lifetime and scope in python all the way through Exercise 1 for good practice.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 18:46

























    answered Nov 13 '18 at 18:33









    MaxMax

    6041824




    6041824













    • What bout this one? x = 1 while x < 10: x = x + 1 print x

      – Gashex
      Nov 13 '18 at 19:00













    • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

      – Gashex
      Nov 13 '18 at 19:19











    • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

      – Gashex
      Nov 13 '18 at 19:22











    • i can't understand your example because in python tabbing is important

      – Max
      Nov 16 '18 at 7:02











    • check [link] (pastebin.com/mUXXdDsw)

      – Gashex
      Nov 16 '18 at 18:19



















    • What bout this one? x = 1 while x < 10: x = x + 1 print x

      – Gashex
      Nov 13 '18 at 19:00













    • The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

      – Gashex
      Nov 13 '18 at 19:19











    • I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

      – Gashex
      Nov 13 '18 at 19:22











    • i can't understand your example because in python tabbing is important

      – Max
      Nov 16 '18 at 7:02











    • check [link] (pastebin.com/mUXXdDsw)

      – Gashex
      Nov 16 '18 at 18:19

















    What bout this one? x = 1 while x < 10: x = x + 1 print x

    – Gashex
    Nov 13 '18 at 19:00







    What bout this one? x = 1 while x < 10: x = x + 1 print x

    – Gashex
    Nov 13 '18 at 19:00















    The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

    – Gashex
    Nov 13 '18 at 19:19





    The results start from 2 to 9 in other code I mentioned in the above comment while the main question code results start from 1 to 9.

    – Gashex
    Nov 13 '18 at 19:19













    I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

    – Gashex
    Nov 13 '18 at 19:22





    I actually understand that basic example 1 but when it comes to while loop related binding things getting messy for me.

    – Gashex
    Nov 13 '18 at 19:22













    i can't understand your example because in python tabbing is important

    – Max
    Nov 16 '18 at 7:02





    i can't understand your example because in python tabbing is important

    – Max
    Nov 16 '18 at 7:02













    check [link] (pastebin.com/mUXXdDsw)

    – Gashex
    Nov 16 '18 at 18:19





    check [link] (pastebin.com/mUXXdDsw)

    – Gashex
    Nov 16 '18 at 18:19













    0














    x = 1 #Declares the variable
    while x < 10: #checks whether the value of x is less than 10 every time the loop runs
    print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)
    x = x + 1 #this is the update part

    Let me tell you one more thing, you do not have any case of global variable here.
    If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link






    share|improve this answer




























      0














      x = 1 #Declares the variable
      while x < 10: #checks whether the value of x is less than 10 every time the loop runs
      print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)
      x = x + 1 #this is the update part

      Let me tell you one more thing, you do not have any case of global variable here.
      If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link






      share|improve this answer


























        0












        0








        0







        x = 1 #Declares the variable
        while x < 10: #checks whether the value of x is less than 10 every time the loop runs
        print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)
        x = x + 1 #this is the update part

        Let me tell you one more thing, you do not have any case of global variable here.
        If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link






        share|improve this answer













        x = 1 #Declares the variable
        while x < 10: #checks whether the value of x is less than 10 every time the loop runs
        print x #prints value of x for the each iterations(1 for first iteration, 2 for second and so on)
        x = x + 1 #this is the update part

        Let me tell you one more thing, you do not have any case of global variable here.
        If you have trouble in understanding whether its global or local declaration, I would recommend you to follow this link







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 18:33









        Sushant DahalSushant Dahal

        162




        162























            -1














            Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.



            This little software helped me to understand behind scenes http://pythontutor.com/visualize.html



            Conclusion:



            It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important






            share|improve this answer


























            • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

              – Thierry Lathuille
              Nov 17 '18 at 22:40











            • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

              – Gashex
              Nov 18 '18 at 5:04











            • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

              – Gashex
              Nov 18 '18 at 5:07











            • @ThierryLathuille Are you there?

              – Gashex
              Dec 1 '18 at 21:38
















            -1














            Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.



            This little software helped me to understand behind scenes http://pythontutor.com/visualize.html



            Conclusion:



            It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important






            share|improve this answer


























            • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

              – Thierry Lathuille
              Nov 17 '18 at 22:40











            • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

              – Gashex
              Nov 18 '18 at 5:04











            • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

              – Gashex
              Nov 18 '18 at 5:07











            • @ThierryLathuille Are you there?

              – Gashex
              Dec 1 '18 at 21:38














            -1












            -1








            -1







            Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.



            This little software helped me to understand behind scenes http://pythontutor.com/visualize.html



            Conclusion:



            It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important






            share|improve this answer















            Counter variable act like a tiny persistent storage. While loop checks whether the stored value in that tiny persistent storage Less than 11. If the value less than 10 then it starts the loop. Inside the loop, it instructs to Print value 1 because it's the current value in that tiny persistent storage after that the other "x" inside loop instruct that tiny persistent storage to update value by adding +1. This loop continues until it gets alert from persistent storage that value stored in persistent storage met 10 value. The loop will successfully exist.



            This little software helped me to understand behind scenes http://pythontutor.com/visualize.html



            Conclusion:



            It seems For loops better for this kind things because it's clean and easy to maintain but understanding this concept is also important







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 18 '18 at 5:04

























            answered Nov 17 '18 at 20:42









            GashexGashex

            76




            76













            • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

              – Thierry Lathuille
              Nov 17 '18 at 22:40











            • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

              – Gashex
              Nov 18 '18 at 5:04











            • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

              – Gashex
              Nov 18 '18 at 5:07











            • @ThierryLathuille Are you there?

              – Gashex
              Dec 1 '18 at 21:38



















            • This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

              – Thierry Lathuille
              Nov 17 '18 at 22:40











            • @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

              – Gashex
              Nov 18 '18 at 5:04











            • @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

              – Gashex
              Nov 18 '18 at 5:07











            • @ThierryLathuille Are you there?

              – Gashex
              Dec 1 '18 at 21:38

















            This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

            – Thierry Lathuille
            Nov 17 '18 at 22:40





            This answer to your own question is ridden with errors and misunderstandings. There is no comparison to 11, only to 10. There is no 'other' x variable, as all of your code is part of the same scope. There is no such thing as an 'alert from persistent storage to the loop'. This shouldn't be the accepted answer.

            – Thierry Lathuille
            Nov 17 '18 at 22:40













            @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

            – Gashex
            Nov 18 '18 at 5:04





            @ThierryLathuille I'm sorry but I made it easier for a newbie to understand. You are correct about 10. I put 11 by mistake. "alert from persistent storage to the loop" means the code check current memory.

            – Gashex
            Nov 18 '18 at 5:04













            @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

            – Gashex
            Nov 18 '18 at 5:07





            @ThierryLathuille What kind of definition you have for other "x' inside the code if it, not a variable?

            – Gashex
            Nov 18 '18 at 5:07













            @ThierryLathuille Are you there?

            – Gashex
            Dec 1 '18 at 21:38





            @ThierryLathuille Are you there?

            – Gashex
            Dec 1 '18 at 21:38


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287106%2fhow-does-a-var-inside-while-loop-intercept-print-function-and-manipulate-outside%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values