0% found this document useful (0 votes)
22 views37 pages

DFC20113 - Chapter 2 (Basic Program Elements - 2.1 - 2.2 - 2.3)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views37 pages

DFC20113 - Chapter 2 (Basic Program Elements - 2.1 - 2.2 - 2.3)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

DFC20113 PROGRAMMING FUNDAMENTALS

Chapter 2
Basic Program
Elements

MADAM SITI ZAHARAH SIDEK


DFC20113 PROGRAMMING FUNDAMENTALS Course Learning Outcome

Upon completion of the course, students should be


able to:

1. Implement programming element and articulate


how they are used to achieve a working program.
( C3, PLO 2 )
2. Show simple programs by developing code to
solve problems in a computer using C++
programming language. ( P2, PLO 3 )
Declare variables and constants with an

Learning appropriate data types.

Initialize variables.
Outcomes
Identify the problem of uninitialized
2.1 variables.

Explain variables Explain keywords.

Implement, write, test and debug program


using data types, variables, constants and
keywords.
Variable
Definition
Before use, variables must be declared. It’s because to tells the
compiler the type of data to store.
Variables are containers for storing data values. This identifier can
change the whose value during the execution of a program.

Declaration
Declaring a variable means specifying both its name and its data
type. In a program, every variable has:

name type value


Variable

Example of variable declaration


Variable

Example of variable declaration


What does a variable
declaration do?
A declaration tells the compiler to allocate enough memory
to hold a value of this data type, and to associate the
identifier with this location.

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

The variable is assigned The variable is assigned a value at the


a value in advance. This run time. The value of this variable can
variable then acts as a be altered every time the program is
constant. being run.
Constant
Definition
Identifier that appear in the program code as fixed values. Any attempt
to modify a constant will result in error.

Types of constant
Constant expression must be initialized when they are declared
and cannot be modified. There are 3 types of constant:

Literal Define Declare


Literal Constant
Literals are the most obvious kind of constants. They are used to express
particular values within the source code of a program.

Example of literal constant


declaration
Define Constant
We can define our own names for constants that we use very often without having to
resort to memory-consuming variables, simply by using the #define preprocessor
directive.

Example of define constant


declaration
Declare Constant
We can declare constants by using const prefix with specific type in the same way as
we would do with variable declaration except that their values cannot be modified
after their definition.

Example of declare constant


declaration
Naming Convention
Rules
Keyword / Reserved Word
Keywords are words that have a special meaning to the computer and cannot be used in
your program for other purpose.
Exercise
Write a variable declaration in static and dynamic initialization
based on the statement below:

1. Declare an integer called sum


2. Declare a character called letter
3. Define a constant called TRUE which has a value of 1
4. Declare a variable called money which can be used to hold currency
5. Declare a variable called number which will hold scientific notation values (+e)
6. Declare an integer variable called total and initialize it to zero.
7. Declare a variable called loop, which can hold an integer value.
8. Declare a constant called GST with a value of .125
Learning 2.5.1 Describe the scope of variables.

Outcomes 2.5.2 Explain the local and global of


variables.

2.5.3 List the differences between


2.2 local and global variables based on
Identify the scope the scope and value after declaration.

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.

3. Identify which is static and dynamic initialization for variables in your


program code with labelled it using comment.

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

10.5 -100.25 20.275


Definition
Double used to declare floating point variable of higher
precision or higher range of numbers
Keyword

double
Sample Value

10.0002755 12E-3
Definition
used to declare single character variable including numeric

Character digits, lowercase/uppercase letters, space (blank) or special


characters(+? “ / ( ) [ ] { } * & %) that enclosed within single
qoute
Keyword

char
Sample Value

'A' '&' '100' 'z' '_'


Definition
used to declare sequence of characters including numeric

String digits, lowercase/uppercase letters, space (blank) or special


characters(+? “ / ( ) [ ] { } * & %) that enclosed within double
qoute
Keyword

string
Sample Value

"Amira" "And&" "number 100"


Definition
Boolean used to declare variable that holding a boolean
value either true or false.
Keyword

bool
Sample Value

bool result = true;


Learning 2.3.1 Identify the syntax used for
Outcomes input and output.

2.3.2 Write programs using input


and output statements. Run test
2.3 and debug the program.
Describe Input and
Output Statement
2.3 Input & Output Statements
Definition
In C++ input and output is performed in the form of a
sequence of bytes or more commonly known as streams.
Input Stream: If the direction of flow of bytes is from
the device(for example, Keyboard) to the main
memory then this process is called input.

Output Stream: If the direction of flow of bytes is


opposite, i.e. from main memory to device( display
screen ) then this process is called output.
Input & Output Statements
cin cout

input output

Program Code

The two keywords cout i and cin in C++ are used


very often for printing outputs and taking inputs
respectively.
Standard Output Stream (cout)
The C++ cout statement is the instance of the ostream class. It is used to produce output on the standard
output device which is usually the display screen. The data needed to be displayed on the screen is inserted in
the standard output stream (cout) using the insertion operator (<<).
Standard Output Stream (cout)
2 Method to Separating Lines of Output:

\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:

Enter number of dragons: _

Underscore above
denotes where keyboard
entry is start.

You might also like