C# Question about Variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

C# Question about Variables

In the 2nd class about C#, it says that "It is called variable because the information stored in that location can be changed when the program is running. " What does this means and how does it works in practice? I mean, when I think about variables, I think about the math ones. So, in those questions about "finding the X", the X is a variable, cause we don't know it's value and can be anyone. But, at the end of the question, we perhaps that "X = 4", so it has a value. When I read this part, I understood that a variable can mean 4 at a time, but it can change to 5 at another time. How does this work?

27th Sep 2018, 5:03 PM
Abel Antonio
Abel Antonio - avatar
2 Answers
+ 1
A variabele is a box. You can put flowers in the box. Get them out of the box and put books in the box. string box; box = "flowers"; Now the box contains flowers box = "books"; Now the box contains books In Math it is almost the same. In one sum x can be 5, in another sum x= 10. A statement is a sum A Math chapter is a program
27th Sep 2018, 7:51 PM
sneeze
sneeze - avatar
0
It is true what they say that the information held by a certain variable can change, and it can happen many times during the lifetime of the program (while it is running). About how it works is a bit hard to explain, because it is contextual. But for the sake of simplicity let's just take an integer for an example. Declare integer named "cost" and give it a value of integer 5; int cost = 5; Then we change information stored in "cost" by giving it another value; cost = 10; By now "cost" is no longer keeping a value of 5 in memory, it is now keeping a value of 10. I hope this explains a little about it, feel free to ask further if necessary, I will try to help, as I can.
27th Sep 2018, 6:09 PM
Ipang