0% found this document useful (0 votes)
2 views38 pages

Comp Prog 2 - Lesson 4

This document provides an overview of C++ basics, focusing on variables, data types, constants, and operators. It includes rules for naming variables, common data types like int, float, char, and bool, as well as the use of keywords and operators in programming. The lesson emphasizes the importance of understanding these foundational concepts for effective coding in C++.

Uploaded by

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

Comp Prog 2 - Lesson 4

This document provides an overview of C++ basics, focusing on variables, data types, constants, and operators. It includes rules for naming variables, common data types like int, float, char, and bool, as well as the use of keywords and operators in programming. The lesson emphasizes the importance of understanding these foundational concepts for effective coding in C++.

Uploaded by

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

LESSON4

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.

Continue to promote the search for truth and knowledge and be


an inspiration for others to follow.

May we be a community of scholars sharing this ambition caring


for one another. And loyal to the truth revealed to us as your
disciples.

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

2. Declaring a statement and making


use of the const command.

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 a = 10; // Assigns 10 to variable a


Mathematical/
Arithmetic Operators
• Used for mathematical calculations.

int sum = 5 + 3; // Addition (8)


int diff = 10 - 4; // Subtraction (6)
int product = 6 * 2; // Multiplication (12)
int quotient = 8 / 2; // Division (4)
int remainder = 10 % 3; // Modulus (1)
Increment & Decrement
Operators
• Used to increase or decrease a variable’s
value by 1

int x = 5;
x++; // Increment (x becomes 6)
x--; // Decrement (x becomes 5 again)
Relational Operators
• Used to compare two values.

int a = 10, b = 20;


bool result1 = (a == b); // False
bool result2 = (a != b); // True
bool result3 = (a > b); // False
bool result4 = (a < b); // True
bool result5 = (a >= b); // False
bool result6 = (a <= b); // True
Logical Operators
• Used for logical operations (AND, OR, NOT).

bool x = true, y = false;


bool result1 = (x && y); // Logical AND
(false)
bool result2 = (x || y); // Logical OR (true)
bool result3 = !x; // Logical NOT
(false)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy