0% found this document useful (0 votes)
7 views27 pages

Fundamentals and Operators

Uploaded by

John Kenly Pamor
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)
7 views27 pages

Fundamentals and Operators

Uploaded by

John Kenly Pamor
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/ 27

CCIT 102 – COMPUTER PROGRAMMING 1

Fundamentals
and
Operators

C A M A R I N E S S U R P O LY T E C H N I C C O L L E G E S | C O L L E G E O F C O M P U T E R S T U D I E S
Learning Objectives
• Outline the program structure of C++ programming language.

• Recognize appropriate coding guidelines for C++ programming


language.

• Write a C++ program as a solution to a given computing problem


that includes the proper implementation of:
1. Variable declaration;
2. Arithmetic expressions;
3. Keywords/constants;
4. Input/output statements;
5. Control flow;
6. Functions;
7. Classes, objects; and
8. Standard template library

• Compile and execute the written program utilizing the appropriate


editor and compiler for C++ programming language.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Our First Program

• Create a blank text file using the Dev C++ editor and
name it as source.cpp
First, let us create an empty C++ program that does nothing.
The content of the source.cpp file is:

The reserved name main is a


int
function name.
main()
Braces mark the beginning
{ and the end of a function
body
}
NOTE:
This simple program does nothing,
it has no parameters listed inside
parentheses, and there are no
The function main is the main program entry statements inside the function
point, the start of our program. When we run body. It is essential to understand
that this is the main program
our executable, the code inside the main signature.
function body gets executed.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Hello World Example
• Now we are ready to get the first glimpse at our “Hello World”
example. The following program is the simplest “Hello World”
example. It prints out Hello World. in the console window:

#include <iostream>

int main()
{
std::cout << "Hello World!";
}

#include <iostream>
-The statement includes the iostream header into our source file via
the #include directive.
std::cout << "Hello World!";
- std::cout allows us to send text to the console, so when we run our
application we see that the text “Hello World!” is printed.
CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER
Other Program Examples

• We can output multiple string literals by separating them with


multiple << operators:
#include <iostream>
int main()
{
std::cout << "Some string." << " Another
string.";
}
• To output on a new line, we need to output a new-line
character \n literal. The characters are enclosed in single quotes
'\n'.
#include <iostream>
int main()
{
std::cout << “First line" << ‘\n’ << " Second
line";
}
CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER
C++ Variables, Literals and
Constants

C++ Variables
• In programming, a variable is a container (storage
area) to hold data.

• To indicate the storage area, each variable should


be given a unique name (identifier). For example,

• int age = 14;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Variables, Literals and
Constants

Rules for Naming a Variable


• A variable name can only have alphabets, numbers, and the
underscore _.

• A variable name cannot begin with a number.

• It is a preferred practice to begin variable names with a


lowercase character. For example, name is preferable to
Name.

• A variable name cannot be a keyword. For example, int is a


keyword that is used to denote integers.

• A variable name can start with an underscore. However, it's


not considered a good practice.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Variables, Literals and
Constants

C++ Literals
• Literals are data used for representing fixed values.
They can be used directly in the code. For example 1,
2.5, 'c' etc.

• Here, 1, 2.5, and 'c' are literals. Why? You cannot


assign different values to these terms.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Variables, Literals and
Constants

C++ Constants
• In C++, we can create variables whose value cannot
be changed. For that, we use the const keyword.
Here's an example:

• const int LIGHT_SPEED = 299792458;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

Types
• Every entity has a type. What is a type? A type is a
set of possible values and operations.
Data Type Meaning
int Integer

float Floating-point
double Double Floating-point
char Character
wchar_t Wide Character
bool Boolean
void Empty

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

1. int
• C++ int

• The int keyword is used to indicate integers.

• Its size is usually 4 bytes. Meaning, it can store values


from -2147483648 to 2147483647.

• For example,
int salary = 85000;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


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. To learn more, visit C++ float and double.

• For example,
float area = 64.74;
double volume = 134.64534;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

3. C++ char
• C++ char

• The keyword char is used for characters.

• Its size is 1 byte.

• Characters in C++ are enclosed inside single quotes '


‘.

• For example,
char test = 'h';

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

4. C++ wchar_t
• Wide character wchar_t is similar to the char data
type, except its size is 2 bytes instead of 1.

• It is used to represent characters that require more


memory to represent them than a single char.

• For example,
wchar_t test = L'‫ 'ם‬// storing Hebrew character;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

5. C++ bool
• The bool data type has one of two possible values:
true or false.

• Booleans are used in conditional statements and


loops (which we will learn in later chapters).

• For example,
bool cond = false;

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++ Data Types

5. 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.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Activity 1

Write a program that declares variables to represent the


length and width of a room. Assign appropriate values
to the variables – for example length = 15 and width =
25. Compute and display the floor space of the room in
square feet (area = length * width). Display explanatory
text with the value – for example, the floor space is 375
square feet. Save it as LASTNAME_Activity1

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Activity 2

Write a program that declares a variable named


minutes, which hold minutes worked on a job and
assign a value. Display the value in hours and minutes.
For example, 197 minutes becomes 3 hours and 17
minutes. Save it as LASTNAME_Activity2

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Activity 3

Write a program that creates variables and values to a


user’s hourly rate of pay and the number of hours
worked. Display the user's gross pay, the withholding
tax(15% of gross pay), and the net pay(gross pay –
withholding). Save it as LASTNAME_Activity3

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Activity 4

Write a program that declares variables and constant


grades on four different subjects in English, Math,
Science and Prog. Find the average. Save it as
LASTNAME_Activity4

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


C++
Operators

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Operators in C++
Operator
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C++ is rich in built-in operators
and provides the following types of operators −

•Arithmetic Operators
•Relational Operators
•Logical Operators
•Bitwise Operators
•Assignment Operators
•Misc Operators

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Operators in C++
Arithmetic Operators
There are the following arithmetic operators supported by C++ language

Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
Modulus Operator and remainder of after an integer
% B % A will give 0
division
++ Increment operator, increases integer value by one A++ will give 11

Decrement operator, decreases integer value by


-- A-- will give 9
one

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Operators in C++
Assignment Operators
There are following assignment operators supported by C++ language −

Operato Description Example


r
= Simple assignment operator, Assigns values from C = A + B will assign
right side operands to left side operand. value of A + B into C

+= Add AND assignment operator, It adds right


C += A is equivalent to
operand to the left operand and assign the result
C=C+A
to left operand.
-= Subtract AND assignment operator, It subtracts
C -= A is equivalent to
right operand from the left operand and assign
C=C-A
the result to left operand.
*= Multiply AND assignment operator, It multiplies
C *= A is equivalent to
right operand with the left operand and assign
C=C*A
the result to left operand.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Operators in C++
Assignment Operators
There are following assignment operators supported by C++ language −

Operato Description Example


r
/= Divide AND assignment operator, It divides left
operand with the right operand and assign the C /= A is equivalent to C = C / A
result to left operand.

%= Modulus AND assignment operator, It takes


modulus using two operands and assign the C %= A is equivalent to C = C %
result to left operand. A

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER


Operators in C++
Relational Operators
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then −
Operato Description Example
r
== Checks if the values of two operands are equal or not, if yes (A == B) is not
then condition becomes true. true.
!= Checks if the values of two operands are equal or not, if (A != B) is true.
values are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value (A > B) is not
of right operand, if yes then condition becomes true. true.

< Checks if the value of left operand is less than the value of (A < B) is true.
right operand, if yes then condition becomes true.

>= Checks if the value of left operand is greater than or equal (A >= B) is not
to the value of right operand, if yes then condition becomes true.
true.
<= Checks if the value of left operand is less than or equal to (A <= B) is true.
the value of right operand, if yes then condition becomes
true.
CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER
Operators in C++
Logical Operators
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then −

Operato Description Example


r
&& Called Logical AND operator. If both the (A && B) is true
operands are non-zero, then condition
becomes true.
|| Called Logical OR Operator. If any of the two (A || B) is true.
operands is non-zero, then condition becomes
true.
! Called Logical NOT Operator. Use to reverses !(A && B) is
the logical state of its operand. If a condition false.
is true, then Logical NOT operator will make
false.

CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER

You might also like