Conditions are phrases that can be evaluated to a boolean value such as a comparison operator between two constants, variables or expressions used to test a dynamic situation. Examples are x <= 5 and bool_flag != true.
Statements are complete program instructions made from constants, variables, expressions and conditions. Statements always end with a semicolon. A program contains one or more statements.
Assignment statements use an assignment operator to store a value or the result of an expression in a variable.
Memory allocation is done at the time of assignment. Primitive datatypes have static allocation with size determined by their type. Simple examples include :
Variables may be assigned an initial value when declared. This is considered good programming practice. Examples are
Array variables can use a shortcut method of initial value assignment. Examples are:
Local variables must be assigned a value prior to use. There is no default assumption. Failure to initialize will cause a compiler error! Field variables (aka properties) have defaults but initialization is good programming practice.
Execution blocks are sets or lists of statements enclosed in curly brackets. Variables maintain their definition (or 'scope') until the end of the execution block that they are defined in. This is the reason why variable declaration and assignment can be a two step process.
Note: It is a good rule of thumb to declare variables in as nested a scope as possible to limit the chance of spurious assignment.
Beware: A variable's content can be hidden by redeclaration of its name within a nested execution block. At times this is convenient but beginning programmers should avoid reuse (ie. 'overload') of variable names.
Statements are complete program instructions made from constants, variables, expressions and conditions. Statements always end with a semicolon. A program contains one or more statements.
Assignment statements use an assignment operator to store a value or the result of an expression in a variable.
Memory allocation is done at the time of assignment. Primitive datatypes have static allocation with size determined by their type. Simple examples include :
first_name = "Fred";
count +=1;
Variables may be assigned an initial value when declared. This is considered good programming practice. Examples are
boolean fileOpenFlag = true;,
int finalScore = null;
final float PI = 3.14159;
Array variables can use a shortcut method of initial value assignment. Examples are:
int v[] = {2,4,20}; //declaration/creation/assignment in one step! int m[][] = {{2,3,4}, {4,5,6}, {1,1,1}}; // two dimensional array
Local variables must be assigned a value prior to use. There is no default assumption. Failure to initialize will cause a compiler error! Field variables (aka properties) have defaults but initialization is good programming practice.
Execution blocks are sets or lists of statements enclosed in curly brackets. Variables maintain their definition (or 'scope') until the end of the execution block that they are defined in. This is the reason why variable declaration and assignment can be a two step process.
Note: It is a good rule of thumb to declare variables in as nested a scope as possible to limit the chance of spurious assignment.
Beware: A variable's content can be hidden by redeclaration of its name within a nested execution block. At times this is convenient but beginning programmers should avoid reuse (ie. 'overload') of variable names.
No comments:
Post a Comment