Computer Programming 1 - Module 1

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Computer Programming 1

Module #1

Name:__________________________
Date: _________________________
Section: _________

Lesson Title: C++ Variables, Literals and Materials:


Constants Lesson Objectives: Student learning module
1. I can relate what is computer programming is all about.
2. I can recognize C++ variables, literals and Constants References:
through examples. www.programiz.com

Productivity Tip:
“Successful and unsuccessful people do not vary greatly in their abilities. They vary in their desires to reach
their potential.” – John Maxwell
“Start strong! Train your brain to shift to work mode by setting a regular time during the day for your lessons.
Set an alarm and stick to your “working hours”

LESSON PREVIEW/REVIEW
Introduction
Steve Jobs once said, "Everybody in this country should learn how
to program a computer... because it teaches you how to think."
Forget the country, follow the rest. Computer programming is an
enormously flexible tool that you can use to do amazing things that
are otherwise either manual and labor some or are just impossible.
If you're using a smartphone, a chat app or if you're unlocking your
car with the push of a button, then you must know that all these
things are using some kind of programming. You're already
immersed in the programs of different types. In fact, software is
running your life. For example, you can write a program that can
automatically respond to every new text message on your phone. To a message like "Hi" or "Hey," the program
can read through the message to detect some pre-defined keywords like "Hi" and "Hey," and send an automatic
response, which could be anything like, "Hi! What's up?" without you needing to see that message. You can even
program it for specific people in your contact list. By learning how to program doesn't mean that you have a
responsibility of creating the next Facebook or the next Dropbox. There was a need - someone suffered from the
lack of something. And then, he dared to address that need because he could. He knew there was a way to solve
that problem and make things easier for himself and probably others. You don't have to learn computer
programming to solve the problems of the world, but you can very well solve yours.
B. MAIN LESSON

Computer programming is a way of giving computers instructions about


what they should do next. These instructions are known as code, and
computer programmers write code to solve problems or perform a task. The
end goal is to create something: that could mean anything from a web page,
or a piece of software, or even just a pretty picture. That’s why computer
programming is often described as a mix between art and science; it’s
technical and analytical, yet creative at the same time.

What is C++ programming?


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.
Data Value
type Variable Assigned

int age = 14;

C++ Variables:
In programming, a variable is a container (storage area) to hold data.
Variable should be given a unique name (identifier). For example,

We will learn about all the data types in detail in the next tutorial.
The value of a variable can be changed, hence the name variable. Note: The int data type suggests that the
variable can only hold integers. Similarly,
int age = 14; // age is 14
we can use the double data type if we have
int age = 17; // age is 17
Rules for naming a variable to store decimals and exponentials.
A variable name can only have alphabets, numbers and the underscore
_. A variable name cannot begin with a number.
Variable names cannot 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 give meaningful names to variables. 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. Example: 1, 2.5, 'c' etc.
Here, 1, 2.5 and 'c' are literals. Why? You cannot assign different values to these terms.
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.
For example: 21, 4, (positive integers) 0, (neither positive nor negative) and -1, -6 (negative integers)
2. Floating-point Literals - is a numeric literal that has either a fractional form or an exponent form.
For example: 5.5 and 0.001
3. Characters - is created by enclosing a single character inside single quotation marks.
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.

5. String Literals - 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
"Earth is round\n" prints string with a newline
C++ Constants
In C++, we can create variables whose value cannot be changed. For that, we use the const keyword.
For example:
const int LIGHT_SPEED = 299792458;
LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant.
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.
For example: #define val 10 (#define identifierName value)

You might also like