Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Variable use issue
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goom




PostPosted: Thu Jun 07, 2018 6:41 pm   Post subject: Variable use issue

What is it you are trying to achieve?
I am trying to make a for loop that assigns different value to a variable based on if the loop is on its first iteration or any of the rest.


What is the problem you are having?
When I run this program I get an error message on the line where I utilize the variable "var variable2 : int := variable * 2", highlighting the variable "variable" and saying that it has not been declared, however if you look at the code it is obvious due to the else that this variable can not go un-declared.


Describe what you have tried to solve this problem
I believe that by bypassing whatever disallows you to run a program with perceived errors would fix this, but I'm not sure how to do that. If I am being dumb in any obvious way please don't be afraid to point that out, cheers!


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

for i : 1 .. 10
if i = 1 then
var variable : int := 1
else
var variable : int := 2
end if
var variable2 : int := variable * 2
put variable2
end for


Turing:

for i : 1 .. 10
    if i = 1 then
        var variable : int := 1
    else
        var variable : int := 2
    end if
    var variable2 : int := variable * 2
    put variable2
end for





Please specify what version of Turing you are using
Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Jun 08, 2018 10:15 am   Post subject: RE:Variable use issue

Variables declared inside something only exist in that something. In this case that something is an if statement. As soon as the if statement finishes, the variable is deleted. Variable2 is declared in a for loop, so as soon as the loop finishes, it gets deleted too. If you need the variable to last, it must be declared before the if/loop/function/etc.

This is a very important concept called scope and it's the same across most programming languages. Large programs may have thousands of variables, and keeping everything organized, remembering what does what, and thinking up unique names for them becomes impossible. If a variable is only used in one small function, we might as well automatically delete it when that function is done, right? Now we can use that name again later, free up some memory, and never think about it again. If a variable is super important for the whole program, we'll declare it at the top, give it a good name, and leave a comment saying what it's for. That variable can be used almost anywhere.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: