Explore 100's Of Free Video Tutorials

Keep Learning, Keep Coding & Keep Sharing

Variables Vs Constants

Notes:

C# variables Vs. constants:
I. Why variables and constants ? : [ Starts at: 00min:00sec]
II. Understanding Variables in C# :[ Starts at: 02min:20sec]
III. Understanding Constants in C# :[ Starts at: 28min:51sec]

I. Why variables and constants ? : [ Starts at: 00min:00sec]

No matter what application we build, we will be dealing with different types of data.
Ex: Game
playerName = "Player1" // store playerName
playerScore = 0; // keep track of playerScore
playerHealth = 3; // keep track of playerHealth
isGameOver = false; // keep track of GameState
where:
playerName is a string type of data
playerScore and playerHealth are number type of data
isGameOver is boolean type of data

To store such data and reuse, we create variables or constants.

II. Understanding Variables in C# :[ Starts at: 02min:20sec]

What is a Variable?:
Variable is a named memory location,
whose value may change during the execution of a program.

Creating variables:
Before using a variable or a constant,
we must declare (create) and initialize it.

Variable declaration and initialization syntax:

// single variable declaration
datatype variableName;

where:
datatype: indicates the type of data we store in a variable
Ex: int, float, char, bool etc.

Naming convention for variables:
(Rules followed to name variables)

Camel Case Naming convention followed to name variables
Keywords should not be used to name a variable
No special characters except underscore is used, not even space
Variable names are case sensitive

// single variable initialization
variableName=value;

// single variable declaration and initialization
datatype variableName = value;

datatype var1,var2,...; // multiple variables declaration
var1=var2=value; // multiple variables initialization

// multiple variables declaration and initialization
datatype variableName1=value, variableName2=value;

Example Code:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
// datatype variableName ;
int numOfBullets; // declaration
numOfBullets = 100; // initialization
int numOfGraneds = 300, totalWeapons; // declaration initialization

totalWeapons = numOfBullets + numOfGraneds;

Debug.Log ("Num of Bullets= " + numOfBullets); // 100
Debug.Log ("Num of Graneds= " + numOfGraneds); // 300
Debug.Log ("Total Weapons= " + totalWeapons); // 400
// player has thrown all graneds
numOfGraneds = 0; // Overwriting value in a variable by assignment
totalWeapons = numOfBullets + numOfGraneds;

Debug.Log ("Num of Bullets= " + numOfBullets); // 100
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
Debug.Log ("Total Weapons= " + totalWeapons); // 100

// player has collected 100 more bullets
numOfBullets = numOfBullets + 100;
totalWeapons = numOfBullets + numOfGraneds;

Debug.Log ("Num of Bullets= " + numOfBullets); // 200
Debug.Log ("Num of Graneds= " + numOfGraneds); // 0
Debug.Log ("Total Weapons= " + totalWeapons); // 200

}
}

III. Understanding Constants in C# :[ Starts at: 28min:51sec]

What is a Constant ?:
Constant is a named memory location,
whose value never change during the execution of a program once it is initialized.

Type of Constants:
C# has compile time ( const ) and run time constants ( readonly ).

Compile time constants:
must and should be initialized when they are declared.

Run time constants:
can be initialized later in constructors at run time
(i.e. after the program gets run).

Constant declaration and initialization syntax:

// declaring and initializing single constant
const datatype CONSTANT_NAME = value;

Naming convention for constants: Rules followed to name constants
All characters in a constant must be capitalized
If there are more than one words, words must be separated by underscore

// declaring and initializing multiple constants
const datatype CONSTANT_NAME1 = value1, CONSTANT_NAME2 = value2,...;

Example Code:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
const int SCREEN_WIDTH = 800 , SCREEN_HEIGHT= 600;
Debug.Log (SCREEN_WIDTH); //800
Debug.Log (SCREEN_HEIGHT); //600
}
}

Note:
1. Whenever we want to store and reuse a data, we create a variable or constant
2. If something may change during the execution, we create a variable
3. Variables can not be used until initialized
4. If something never change during the execution, we create constant
5. Constants must and should be initialized when declared