+ 1
My kryptonite so far
Not a question, just more of a comment. I'm starting from scratch, and variables are by far the most confusing thing for me
5 Antworten
+ 4
Why are they confusing for you?
+ 1
variable is easy, loop is hard.
0
you can consider variables as boxes.
When you create a variable like this :
int a = 5;
It's like if you were creating a box called a with inside the value 5.
When you do this :
int b = a*3;
You calculate the value of 3*a your computer is going to say well Im going to check in the box called a what there is ... Oh there is a 5 then 5*3=15, okay ! so i create a new box called b with the value of 15 inside.
Then if you do this :
a = 1
Your computer says : "ok so i first check if a have a box called a, okay i found it ! i will just delete all the data there is in the variable : in the fire, and i will put 1 in the box instead.
Note that b will still be 15 it's not because it was b = 5*a that when you change the a value you change the b value. There are not related.
The comparaison between boxes and variables is useful to understand the basics but also to deal with more complicated concepts like pointers.
0
Variables are the basics. if you are getting confused then it would be rather difficult. Think of variables as some EMPTY BOX that can vary. That means practically you can always shift things from 1 box to another box.
Suppose,
int a,b; \\are two variables
a=5; \\Put a value 5 in a box called 'a'
b=3+a; \\Assigned 3+a in a box called 'b'.But a=5 (already defined). So value inside becomes 3+5=8
a=1; \\ Now this line means the compiler is told to remove the old value of 5 from box 'a' and put a new value 1
Hope you get what are variables...
- 1
Variable is too easy to deal with it. Think you have your own people , and you give one of them 10 dollars ( as an integar) or give him a nice name ( as a string ) . Sometimes you think you need to take 5 dollars then you should change what you assign to him or even give hime a new name so you also need to change his name by updating the Var .. first time it seems to hard but trust me it is so easy go and practice more until you understand ans ask more 😁