• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning local variables after loop  RSS feed

 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i initilize the local varable before a loop
and the loop supposed to change the value of the local variable
when i return the local variable

it returns its initilization before excuting the loop


here is an example




why ???




 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and welcome to the Ranch! Please read UseCodeTags (<-click) carefully.

I suppose you didn't want your for loop to look like this:

because the code in this loop never gets executed in your example (5 < 3 is false), hence the result you get. Instead:

would give you the result you expect, I guess.
 
amr talaat
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:Hello and welcome to the Ranch! Please read UseCodeTags (<-click) carefully.

I suppose you didn't want your for loop to look like this:

because the code in this loop never gets executed in your example (5 < 3 is false), hence the result you get. Instead:

would give you the result you expect, I guess.


ooh thanks silly mistake
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kemal Sokolovic wrote:Hello and welcome to the Ranch!
Welcome again
Please read UseCodeTags (<-click) carefully.
. . . .
I have added he code tags for you, since you are new here, and also removed the unnecessary blank lines, and you can see how much better it looks
 
B Smith
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Others have already pointed out that the problem comes from using the wrong variable name. I'd just add that this is a good example for why you should use good variable names rather than just letters, consider:





Bit easier to spot the mistake in the second one, isn't it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!