• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

returning local variables after loop

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???




 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic