understand the variables and types in programming
c# unity learn » Devlog
what does it mean by
VARIABLE and TYPE ???
Variables are simply containers for storing data values(exp : names ,numbers ,characters..etc)
Now not all the barrels have the same color ,each color refer to a certain “TYPE” of data that the barrel is designed to store
For exemple :
Explanation of the exemple :
1-green barrel can store integers(…-3,-2,-1,0,1 ,2,3..)
2-the red barrel can store text (words , paragraphs..)
3-blue barrel can store characters(‘A’,’B’…,’?’,’/’..’a’,’b’..)
----------- Analogy :
A BARREL means VARIABLE
The COLOR of the barrel means the TYPE of the variable
And now let rewrite these previous phrases :
1-the INT variable can store integers(…-3,-2,-1,0,1 ,2,3..)
2-The STRING variable can store text (words , paragraphs..)
3-the CHAR variable can store characters(‘A’,’B’…,’?’,’/’..’a’,’b’..)
-----------SYNTAX :
Now let try to code an example in c#
Before we dive into the syntax word I want to mention that each variable have a DISTINCT name for it. why ? imagine if your friend doesn’t have a name then how can you call him/her ?
creating a variable in c# language :( 2 steps)
syntax | exemple | |
1 - DECLARING A variable : | type variable_name ; | |
2-INITIALISING a variable (assign a value to a variable ) | variable_name =data_value; |
Or you can mix the two steps in one line of code :
type variable_name = data_value; |
Now you are wondering “what the heck is that semi_colon in the end of every line of code ?”
the answer : is exactly like the full stop at the end of the sentence . the semi colon “;” means that the command(line of code) is done.
------other types of variables:
Boolean :
the variable of type Boolean can only store one of two values {true , false} (in lowercase ,yes there is a difference between lower and uppercase in programming)
Exp :
Now you will tell me : “hey wait ! aren't true and false words , and we said previously that variables that store text should have the type of string not Bool !
I ll answer and tell you :”brilliant question but true and false are not ordinary words they are KEYWORDS,”
-what are keywords ?
-simply they are predefined, reserved words used in programming that have special meanings in the programming language like :
-so what happens if I wanted to use the word true or false in my text ? I can’t?
-no you can ! the thing that differentiate the text words from the keywords is just the double quotes {“”} let ‘s see an exemple
Float :
type of a variable that stores a number that has a decimal place
Note :
=> the first is considered as integer and the second is considered as float(3.0)
----------------------------------------------------------------------------------------------------------------
CONCLUSION :
variables are just containers for storing
data ,each variable have a name and type and a value .
--------------------------------------------------------------------------------------------------------------
Note : you should initialize(assign a value to) your variable before using it or you will get this error message :” Use of unassigned local variable” or something like it.
In the next devlog we will take a look of some
operations and applications of the variables
If you have any questions feel free to post them in the comments and I ll be happy to answer them!
and if you liked this support me by following me
thank you for reading!!!
c# unity learn
learn c# coding with visual representations to make your game in unity
Status | In development |
Author | yassinealoui |
Tags | coding, c-sharp, explain, game-development, programming, Tutorial |
More posts
- learn coding for your games in a different way(c#)Jun 02, 2020
Leave a comment
Log in with itch.io to leave a comment.