DFC20113 - Chapter 2 (Basic Program Elements - 2.1 - 2.2 - 2.3)
DFC20113 - Chapter 2 (Basic Program Elements - 2.1 - 2.2 - 2.3)
Chapter 2
Basic Program
Elements
Initialize variables.
Outcomes
Identify the problem of uninitialized
2.1 variables.
Declaration
Declaring a variable means specifying both its name and its data
type. In a program, every variable has:
DECLARE ALLOCATE
VARIABLE MEMORY
Variable
Initialization
The variables once declared, are assigned some value. This assignment of value to these variables is called
initialization of variables.
Static Dynamic
Initialization Initialization
Types of constant
Constant expression must be initialized when they are declared
and cannot be modified. There are 3 types of constant:
of variables
Scope of Variables
In programming, the scope of a variable is defined as the extent of the program code
within which the variable can we accessed or declared or worked with.
Let's watch
this video
Scope of Variables
Scope of Variables
Activity
1. Cut and rearrange the given program code based on the statement
below and paste it on the paper provided:
Declare the global variable length and width.
Declare the local variable length, width and area.
Assign value 5.5 and 3.0 to length and width respectively.
Display statement to calculate area for global variable.
Ask user to input length and width.
Process the calculation of area for global variable.
Display the area of rectangle for global variable.
Display statement to calculate area for local variable. Time
Process the calculation of area for local variable. Allocation:
Display the area of rectangle for local variable. 40 minutes
Activity
2. Open DEV C++ IDE, write the complete program code that has been
arranged and display the output.
Enjoy
it!
Conclusion
The local and global variables both are necessary and equally
required while writing the program. However, declaring a large
number of global variables could be problematic in a massive
program, as it may cause unwanted changes to a global variable;
and it would become hard to identify that which part of a
program made that change. So, we should avoid declaring
unnecessary global variables.
Data Types
Data type is classification of a particular type of information. Data types are essential to any
computer programming language. Different data types have different sizes in memory
depending on the machine and compilers
Definition
Integer used to declare numeric program variables
including whole numbers, positive and negative.
Keyword
int
Sample Value
10 -100 20 -200
Definition
Float used to declare real numbers with a decimal point
including whole numbers, positive and negative.
Keyword
float
Sample Value
double
Sample Value
10.0002755 12E-3
Definition
used to declare single character variable including numeric
char
Sample Value
string
Sample Value
bool
Sample Value
input output
Program Code
\n endl
Example
Standard Input Stream (cin)
The C++ cin statement is the instance of the class istream and is used to
read input from the standard input device which is usually a keyboard.
The extraction operator(>>) is used along with the object cin for reading
inputs.
Standard Input Stream (cin)
Example
Note no “\n" in cout. Prompt "waits"
on same line for keyboard input as
follows:
Underscore above
denotes where keyboard
entry is start.