0% found this document useful (0 votes)
27 views

Lesson 2 - Programming Languages

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lesson 2 - Programming Languages

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

URDANETA CITY

UNIVERSITY
Owned and operated by the City Government of Urdaneta

PROGRAMMING LANGUAGE C++

Learning Objectives
At the end of the module, the I.T. students will be able to:
• Understand the concepts of programming languages.
• Giving the student a foundation in C++ programming, with an emphasis on learning
basic programming principles such as I/O operations, variable management, flow
control, and implementation of OOP principles such as classes and inheritance, as
well as more advanced capabilities such as working with files and network basic
operations using the TCP and UDP protocols.

What is C++?
C++ is a powerful general-purpose programming language. It can be used to develop
operating systems, browsers, games, and so on. C++ supports different ways of programming
like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as
flexible.

C++ Variables, Literals and Constants


In this module, we will learn about variables, literals, and constants in C++ with the help of
examples.

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,

Here, age is a variable of the int data type, and we have assigned an integer value 14 to it.

Note: The int data type suggests that the variable can only hold integers. Similarly, we can
use the double data type if we have to store decimals and exponentials.

We will learn about all the data types in detail in the next discussions. The value of a variable
can be changed, hence the name variable.

Rules for naming a variable


• A variable name can only have alphabets, numbers, and the underscore _.
• A variable name cannot begin with a number.
• Variable names should not begin with an uppercase character.
• 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.
Note: We should try to give meaningful names to variables. For example, first_name is a
better variable name than fn.

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.

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

Here's a list of different literals in C++ programming.

1. Integers
An integer is a numeric literal(associated with numbers) without any fractional or
exponential part. There are three types of integer literals in C programming:
• decimal (base 10)
• octal (base 8)
• hexadecimal (base 16)

For example:

In C++ programming, octal starts with a 0, and hexadecimal starts with a 0x.

2. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponent
form. For example:
• -2.0
• 0.0000234
• -0.22E-5

Note: E-5 = 10-5

3. Characters
A character literal is created by enclosing a single character inside single quotation marks.
For example: 'a', 'm', 'F', '2', '}' etc.

4. Escape Sequences
Sometimes, it is necessary to use characters that cannot be typed or has special meaning in
C++ programming. For example, newline (enter), tab, question mark, etc.

In order to use these characters, escape sequences are used.


Escape Sequences Characters
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null Character

5. String Literals
A string literal is a sequence of characters enclosed in double-quote marks. For example:

"good" string constant


"" null string constant
"" string constant of six white space
"x" string constant having a single character

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

"Earth is prints string with a newline


round\n"

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

Here, we have used the keyword const to declare a constant named LIGHT_SPEED. If we try
to change the value of LIGHT_SPEED, we will get an error.

A constant can also be created using the #define preprocessor directive. We will learn about
it in detail in the C++ Macros tutorial.

To start using C++, you need two things:


• A text editor, like Notepad, to write C++ code
• A compiler, like GCC, to translate the C++ code into a language that the computer will
understand
There are many text editors and compilers to choose from. In this tutorial, we will use an IDE
(see below).

C++ Install IDE


An IDE (Integrated Development Environment) is used to edit AND compile the code. Popular
IDE's include Code::Blocks, Eclipse, and Visual Studio. These are all free, and they can be used
to both edit and debug C++ code.
Note: Web-based IDE's can work as well, but functionality is limited.
Note: as I’ve said in our class orientation any you are free to use any text editor available and
even mobile application will do as long as it can fit in our subject.

C++ Quickstart
Let's create our first C++ file.
• Open Codeblocks and go to File > New > Empty File.
• Write the following C++ code and save the file as myfirstprogram.cpp (File > Save File
as):

In Codeblocks, it should look like this:

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

Then, go to Build > Build and Run to run (execute) the program. The result will look
something to this:

C++ Syntax
Let's break up the following code to understand it better:

Example explained
• Line 1: #include <iostream> is a header file library that lets us work with input and
output objects, such as cout (used in line 5). Header files add functionality to C++
programs.
• Line 2: using namespace std means that we can use names for objects and variables
from the standard library.
• Line 3: A blank line. C++ ignores white space.
• Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function.
Any code inside its curly brackets {} will be executed.
• Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<)
to output/print text. In our example it will output "Hello World".

Note: Every C++ statement ends with a semicolon ;.


Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }

Remember: The compiler ignores white spaces. However, multiple lines makes the code
more readable.

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

• Line 6: return 0 ends the main function.


• Line 7: Do not forget to add the closing curly bracket } to actually end the main function.

Omitting Namespace
You might see some C++ programs that runs without the standard namespace library.
The using namespace std line can be omitted and replaced with the std keyword, followed
by the :: operator for some objects:

C++ Output (Print Text)


The cout object, together with the << operator, is used to output values/print text:

Example: Result:

You can add as many cout objects as you want. However, note that it does not insert a new
line at the end of the output:

Example: Result:

C++ Output (Print Text - New Lines)


To insert a new line, you can use the \n character:

Example: Result:

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

Tip: Two \n characters after each other will create a blank line:

Example: Result:

Another way to insert a new line, is with the endl manipulator:

Example: Result:

Learning Activities/Tasks*
• Search and list down editors that can run or compile C++.
• Create a simple C++ program that display the college and university VMGO.
A screenshot and output of your activity will be collected next meeting.

Learning Resources
C++ Introduction. (2011). Retrieved October 6, 2021, from W3schools.com website:
https://www.w3schools.com/cpp/cpp_intro.asp
C++ Variables, Literals and Constants. (2021). Retrieved October 6, 2021, from
Programiz.com website: https://www.programiz.com/cpp-
programming/variables-literals

Compiled by:

CHRISTIAN C. MEQUIN
Subject Instructor

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]
URDANETA CITY
UNIVERSITY
Owned and operated by the City Government of Urdaneta

ELMER D. VALDEZ
Subject Instructor

(075) 600 - 1507


San Vicente West, Urdaneta City, Pangasinan
Bright future starts here ucu.edu.ph | [email protected]

You might also like