Toggle menu
862
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Variables: Difference between revisions

From Catglobe Wiki
Tungocman (talk | contribs)
Tungocman (talk | contribs)
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:
=Variables=
=Variables=


'''Declaring a Variable'''
===Declaring a Variable===


CGS variables are declared using this form of statement:
CGS variables are declared using this form of statement:


''datatype var_name;''
[[:Category:Data_Types_Literals_and_Variables|data_type]] var_name;


where datatype is the data type of the variable and var_name is its name. You can declare a variable of any valid types. When you create a variable, you are creating an instance of its type. Thus, the capabilities of a variable are determined by its type, and the type of a variable cannot change during its lifetime.
where datatype is the data type of the variable and var_name is its name. You can declare a variable of any valid types. When you create a variable, you are creating an instance of its type. Thus, the capabilities of a variable are determined by its type, and the type of a variable cannot change during its lifetime.
Line 14: Line 14:
All variables in CGS must be declared prior to their use.
All variables in CGS must be declared prior to their use.


'''Initializing a Variable'''
===Initializing a Variable===


You must give a variable a value prior to using it. One way to do this is through an assignment statement (see Operators - The Assignment Operator). Another way is by giving it an initial value when it is declared. The general form of initialization is shown here:
You must give a variable a value prior to using it. One way to do this is through an assignment statement (see Operators - The Assignment Operator). Another way is by giving it an initial value when it is declared. The general form of initialization is shown here:


''type_name var_name = value;''
[[:Category:Data_Types_Literals_and_Variables|data_type]] var_name = value;


Here, value is the value that is given to var_name when var_name is created. The value must be of the type type_name.
Here, value is the value that is given to var_name when var_name is created. The value must be of the type data_type.


Here are some examples:
===Examples===


''number n = 1;''
number n = 1;


''string s = "hello";''
string s = "hello";
__NOTOC__
__NOTOC__
<!-- imported from file: 239.htm-->

Latest revision as of 09:41, 16 December 2011



Variables

Declaring a Variable

CGS variables are declared using this form of statement:

data_type var_name;

where datatype is the data type of the variable and var_name is its name. You can declare a variable of any valid types. When you create a variable, you are creating an instance of its type. Thus, the capabilities of a variable are determined by its type, and the type of a variable cannot change during its lifetime.

All variables in CGS must be declared prior to their use.

Initializing a Variable

You must give a variable a value prior to using it. One way to do this is through an assignment statement (see Operators - The Assignment Operator). Another way is by giving it an initial value when it is declared. The general form of initialization is shown here:

data_type var_name = value;

Here, value is the value that is given to var_name when var_name is created. The value must be of the type data_type.

Examples

number n = 1;

string s = "hello";