• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why does this code work

 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This could should obtain a few words and translate them from one language to another. i got this code from my teacher and i dont understand a few things





Here is the second part of the code:

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First part:

Line 39: I don't understand what you're asking there. 'word' is filled by concatenating letters in the loop on lines 34 - 38.

Line 44: If both i and count are 0, then the loop will stop at the beginning (it won't run at all). However, most of the time, count will not be 0. Note that count is incremented in line 20.

Second part:

Line 9: The loop advances 'index' to the first character in 'sentence' that is not a space (note that line 10 is the body of the loop).

Line 18: The variable 'found' is used to keep track of if the content of 'word' is found in the array 'ger'; lines 19 - 23 loop over 'ger' to see if 'word' is in there, and if so, then 'found' is set to true.

Line 25: 'found' is not a method, it's a variable. It will be true when 'word' was found in the array 'ger', and false if it was not found (see above).
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:First part:

Line 39: I don't understand what you're asking there. 'word' is filled by concatenating letters in the loop on lines 34 - 38.

Line 44: If both i and count are 0, then the loop will stop at the beginning (it won't run at all). However, most of the time, count will not be 0. Note that count is incremented in line 20.

Second part:

Line 9: The loop advances 'index' to the first character in 'sentence' that is not a space (note that line 10 is the body of the loop).

Line 18: The variable 'found' is used to keep track of if the content of 'word' is found in the array 'ger'; lines 19 - 23 loop over 'ger' to see if 'word' is in there, and if so, then 'found' is set to true.

Line 25: 'found' is not a method, it's a variable. It will be true when 'word' was found in the array 'ger', and false if it was not found (see above).


what i dont understand, with "word" and "found" variable.. if it is defined in and outside of the looop.. why the variable doesnt die being inside the loop..
i did this code, and when i set my variable word.. i set it like this word="";
and i defined second time inside the loop..
but when i was refering to it, it referred to the variable first defined.. word=""

how does the program selects which variable to refer to , if the same variable is defined twice
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You define the variable 'word' in line 10 - which is the one that is been updated/reassigned in the first part in line 12, 18, 33, 36, 46 and 49 - and in the second part in line 8 and 14.

I can't see where you define the boolean variable 'found' - but it is the same that is been updated/reassigned in line 18 and 22 - and it is been used in line 25.
 
Dmitri Makovetskiy
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene Larsen wrote:You define the variable 'word' in line 10 - which is the one that is been updated/reassigned in the first part in line 12, 18, 33, 36, 46 and 49 - and in the second part in line 8 and 14.

I can't see where you define the boolean variable 'found' - but it is the same that is been updated/reassigned in line 18 and 22 - and it is been used in line 25.



why wont all those variables inside the loop die and outside the loop dominate ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's the same variable. You're not declaring a new one. It's the same one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic