Computer Fundamentals
and Programming
Lecture
Module 2
Declaring Variables
Objectives:
At the end of this lesson, students will be able to :
• Students will be able to identify and explain the
different data types in C++
• Students will learn how to declare variables in
C++, including syntax
• students will understand the concept of variable
scope (local vs. global variables) and how it
affects accessibility within a program.
• Students will practice using declared variables in
expressions and calculations.
Comments
Comments are essential in programming as they help explain the
code, making it easier to understand and maintain.
In C++, comments can be used to describe what your code does, to
leave notes for yourself or others, and to temporarily disable code
during debugging.
1. Single-line Comments
Single-line comments start with //.
Everything following // on that line will be ignored by the compiler.
.
2. Multi-line Comments
Multi-line comments start with /* and end with */.
They can span multiple lines and are useful for longer
explanations.
Variable
A variable is a named storage
location in memory that can hold
a value.
In C++, we declare a variable
before using it, specifying its type
and name.
Common Data types
• Integer (int)
• Floating Point (float, double)
• Character (char)
• Boolean (bool)
1. Integer (int)
The int type is used to store whole
numbers.
Example
int age = 25;
Give an example of
an integer variable
2. Floating Point (float and double)
float is used for single-precision floating-point numbers,
while double is for double-precision.
Example
float price = 19.99f; // Single precision
double distance = 12345.6789; // Double precision
Give an example of a
float variable.
3. Character (char)
The char type is used to store a single character.
Example:
char initial = 'A';
Give an example of a
character variable.
4. Boolean (bool)
The bool type is used to store truth values: true or false.
Example:
bool isStudent = true;
#include <iostream>
using namespace std; // Use the standard namespace
int main() {
// Declaring variables
int numStudents = 30; // Integer variable
float height = 5.9f; // Float variable
double weight = 70.5; // Double variable
char grade = 'A'; // Character variable
// Output the values of the variables
cout << "Number of Students: " << numStudents << endl;
cout << "Height: " << height << " feet" << endl;
cout << "Weight: " << weight << " kg" << endl;
cout << "Grade: " << grade << endl;
return 0;
}
If you remove endl; from the output statements in the C++ program, the
output will still be displayed, but it will not automatically move to the
next line after each output. Instead, all the outputs will be printed on
the same line.
Without endl;
Number of Students: 30 Height: 5.9 feet Weight: 70.5 kg Grade: A
With endl;
Number of Students: 30
Height: 5.9 feet
Weight: 70.5 kg
Grade: A
Has Graduated: No
cout
cout (short for "character output") is an object in C++ used for
outputting data to the standard output stream, usually the
console.
It is part of the iostream library, which must be included at the
beginning of your program.
The basic syntax of using cout is:
<< is the stream insertion operator that directs the output to the cout stream
Common Uses of cout
• Printing Text: You can print strings enclosed in double quotes.
• Printing Variables: You can print the values of variables.
• Combining Text and Variables: You can combine text and variables in a single
output statement.
Example
cout << "Hello, World!" << endl; // Printing a simple message
cout << "Age: " << age << endl; // Printing an integer variable
cout << "Height: " << height << " feet" << endl; // Printing a float variable
In C++, arithmetic operators are used to perform
mathematical operations on variables and values.
There are five primary arithmetic operators:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Modulus (%) – returns the remainder of a division
operation.
Example
You can use \n to insert new lines instead of using std::endl.
Make a c++ program to compute the
Compute the interest of a 150,000 pesos
payable for 10 years at the rate of 5%?
Make a c++ program to compute the
1. Area of rectangle
2. Area of circle pi x r2
3. Perimeter of a rectangle
4. Circumference of a Circle 2*pi*r
5. Conversion of peso to dollar
6. dollar to peso
6. feet to inch
7. cm to meter
8. minute to hour
9. hour to minutes
10. Salary of an employee given the number of hours per week and rate per day of
the employee. Compute for the salary per month.
Thank you. End of lesson.