Comp Prog 2 - Lesson 4
Comp Prog 2 - Lesson 4
LESSON4
OPENING PRAYER
Lord God of all wisdom, We pray for La Consolacion University
Philippines that she may be faithful to the purposes of our
foundresses, Mo. Rita & Venerable Mo. Consuelo.
AMEN.
GOOD MORNING
REVIEW.
What was our last lesson all about?
LESSON4:
C++ BASICS
GLOBAL
DECLARATION
GLOBAL
DECLARATION
• contains user-defined variables
and constants.
variable.
VARIABLE
• in programming, a variable is a
container (storage area) to hold
changeable data.
• to indicate the storage area, each
variable should be given a unique
name (identifier).
VARIABLE
EXAMPLE:
int age;
DATA TYPE
VARIABLE
VARIABLE
EXAMPLE:
int age;
DATA TYPE
VARIABLE
VARIABLE
EXAMPLE:
color =
age = 18;
blue;
sum = 15;
grade = F;
RULES
NAMING A VARIABLE
RULES
NAMING A
VARIABLE
1. A variable name can only have
alphabet, numbers and the
underscore.
2. A variable names cannot begin
with a number.
3. Variable names cannot begin
with an uppercase character.
RULES
NAMING A
4. A variable
VARIABLE
name cannot be a
keyword. For example. int is a
keyword that is used to denote
integers.
5. A variable name can start with
an underscore. However, it's not
considered a good practice.
6. There should be no white space.
Use underscore or dont use
space if it is a two or more word
RULES
NAMING A
4. A variable
VARIABLE
name cannot be a
keyword. For example. int is a
keyword that is used to denote
integers.
5. A variable name can start with
an underscore. However, it's not
considered a good practice.
6. There should be no white space.
Use underscore or dont use
space if it is a two or more word
DATA TYPES
When we declare variables, we
also declare the variable’s data
type. The Data Type of a variable
determines what kind of values it
can store.
common C++
DATA TYPES
1.• The
C++ int
int keyword is used to
indicate integers.
• Its size is usually 4 bytes.
Meaning, it can store values
from -2147483648 to
2147483647.
• For example:
common C++
DATA TYPES
2. C++ float and double
• float and double are used to store
floating-point numbers (decimals
and exponentials).
• The size of float is 4 bytes and the
size of double is 8 bytes. Hence,
double has two times the precision
of float.
• For example,
common C++
DATA TYPES
3. C++ char
• Keyword char is used for
characters.
• Its size is 1 byte.
• Characters in C++ are enclosed
inside single quotes ' '.
• For example,
common C++
DATA TYPES
4. C++ bool
• The bool data type has one of two
possible values: true or false.
• Booleans are used in conditional
statements and loops.
• For example,
common C++
DATA TYPES
4. C++ void
• The void keyword indicates an
absence of data. It means
"nothing" or "no value".
• We will use void when we learn
about functions and pointers
keywords.
KEYWORDS
• are reserved words in a
programming language that
have specific uses, and for
executing a command.
• Examples: if, if-else, do-while,
switch, main(), printf, cout
constants.
CONSTANTS
• Like variables, contants also act like
containers which are user-define and
are givenn intial values. However, the
values of contants cannot change
during the program.
2 WAYS TO DECLARE CONSTANT
1. Declaring a statement under the
preprocessor directives and making
use of the command #define.
Example:
2 WAYS TO DECLARE CONSTANT
Example:
operators.
OPERATORS
• this are symbols that execute a
corresponding action during the
program.
TYPES OF OPERATORS
• ASSIGNMENT OPERATOR
• MATHEMATICAL/ARITHMETIC OPERATORS
• INCREMENT & DECREMENT OPERATORS
• RELATIONAL OPERATORS
• LOGICAL OPERATORS
Assignment Operator
• Used to assign values to variables.
int x = 5;
x++; // Increment (x becomes 6)
x--; // Decrement (x becomes 5 again)
Relational Operators
• Used to compare two values.