You are on page 1of 7

MODULE 3 – INTRODUCTION TO ELEMENTS OF A PROGRAM part 2 2.

2. On the first page of the lesson, you will find the specific learning outcomes (SLOs) of each
lesson. SLOs are knowledge and skills you are expected to acquire at the end of the lesson. Read them
heartily.
I. INTRODUCTION 3. You must answer the Learning Activities/Exercises (LAEs). The LAEs are designed to help
Variables, constants, operators, and expressions. We can find these words in mathematics, and you acquire the SLOs.
in programming these are used not so differently. Variables are still used for substitution of values. 4. Feel free to chat, call, text (09069352363) or send an email (tedelricot@gmail.com) message
Constants remain the same through and through. Operators however, are not only arithmetic but there to me if you have questions, reactions, or reflections about the contents or activities in the module.
are different types to take into consideration for programming, including expressions. Let us learn together 5. The Practice Task/Assessment and the Assignment shall be checked by me.
their similarities and differences.
V. LESSON 1: Variables and Constants
II. COURSE LEARNING OUTCOMES
1. Participate in the generation of new knowledge of fundamental principle of programming in A. Specific Learning Outcomes
development projects. At the end of the lesson you will be able to:
2. Respond to computer programming activities with substantial degree of independence as an 1. differentiate variables from constants
individual or a team. 2. identify the interoperability of variables along with constants
3. Defend a programming project implemented as a solution to a given problem in Computer 3. enumerate the different variable types
Science.
4. Articulate the latest developments of algorithms that maybe implemented as a solution to a B. Motivation/Prompting Question (s)
given problem. Variables and constants. Sounds like algebra, right? Well indeed we have to recall our basic
5. Embody different roles within a team in testing, and debugging a program. knowledge of algebra when we are doing a code to solve a problem. This is why we, as “Scientists”, seek
6. Apply knowledge of science on fundamental programming components appropriate to to provide solutions to problems using computers. This is why we are called, Computer Scientists. Do
Computer Science. you know the basic reason why there are variables in programs? Or the reason why there are constants
7. Apply knowledge of basic computing appropriate to Computer Science. as well? If you wanna know so badly, it is explained in this lesson, and shall be further explained with
8. Apply knowledge of integrated development environment through algorithms that maybe practical examples in online meetings.
implemented as a solution to a given problem.
III. CONTENTS OF THE MODULE C. Discussion
Lesson 1: Variables and constants C.1. Variables
Lesson 2: Operators and expressions When a program accepts input, the first thing the computer needs to do is find
IV. HOW TO USE THIS MODULE a place to store any data that it gets from the user. Because computers are like one
This module will benefit you much through following all points carefully. The necessary key giant brain, they simply store data in memory. A variable simply acts like a storage bin.
points for you to familiarize are summarized as follows:
You can stuff any type of data into a variable, such as numbers or words, and then
1. This module contains one (1) lesson. The lesson is explained substantively. Read the
retrieve them back out again so you can stuff different data into that variable all over
explanations thoroughly so that you could understand the lesson fully.
again.

Prepared by: Mr. Ted Elrico R. Tamparong


C.1.a Local Variables C.2. Constants
Variables that are declared inside a function or block are local variables. Constants refer to fixed values that the program may not alter and they are called literals.
They can be used only by statements that are inside that function or block of Constants can be of any of the basic data types and can be divided into Integer Numerals,
code. Local variables are not known to functions outside their own. Floating-Point Numerals, Characters, Strings and Boolean Values. Again, constants are treated
(tutorialspoint, 2014). just like regular variables except that their values cannot be modified after their definition.
Local variables are exclusive only to the function declaring them. (tutorialspoint, 2014).
Hence, it becomes useless or invisible to other functions. Constants’ values remain the same throughout the execution of the code.
C.1.b Global Variables Example:
Global variables are defined outside of all the functions, usually on top
of the program. The global variables will hold their value throughout the life-
time of your program. A global variable can be accessed by any function. That
is, a global variable is available for use throughout your entire program after its
declaration. (tutorialspoint, 2014).
These variables are so popular, any function can use them throughout
the execution of the code. Because of this reason, it can change its values
C.1.Example

D. Learning Activities/Exercises
1.) How are variables and constants:
a.) Similar?
b.) Different?
2.) How do variables and constants interact?
3.) Enumerate and briefly define in your own words the types of variables.

Prepared by: Mr. Ted Elrico R. Tamparong


E. Teacher Intervention
For clarifications, feel free to chat, call, text (09069352363) or send an email C.1.b Relational Operators
(tedelricot@gmail.com) message to me if you have questions, reactions, or reflections about the Operator Description Example
contents or activities in the lesson. == 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
VI. LESSON 2: Operators and Expressions (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
A. Specific Learning Outcomes of right operand, if yes then condition becomes true. true.
At the end of the lesson you will be able to:
< Checks if the value of left operand is less than the value of
1. identify the purpose of each operator (A < B) is true.
right operand, if yes then condition becomes true.
2. manipulate operators according to their uses
>= Checks if the value of left operand is greater than or equal
3. make expressions using proper operators. (A >= B) is not
to the value of right operand, if yes then condition becomes
true.
true.
B. Motivation/Prompting Question (s) <= Checks if the value of left operand is less than or equal to
“Hello operator?”. Is a common telephone spiel, but have you wondered how operators work in the value of right operand, if yes then condition becomes (A <= B) is true.
C++? Do you know how many they are? Are you eager to learn how to use them? By now, you know how true.
statements are constructed by the example of simple statement. Now you will learn how operators work
in complex statements.
C.1.c Logical Operators
C. Discussion
Assume A = 1 and B = 0;
C.1 Operators (according to tutorialspoint, 2014) Operator Description Example
An operator is a symbol that tells the compiler to perform specific mathematical
Called logical AND operator. If both are non-zero, then
or logical manipulations. && (A && B) is false
condition becomes true.
Assume variable A holds 10 and variable B holds 20
Called logical OR operator. If any of the two operands is
C.1.a Arithmetic Operators || (A || B) is true
non-zero, then condition becomes true.
Operator Description Example
+ Adds two operands A + B will give 30 Called logical NOT operator. Use to reverse the logical
- Subtracts second operand from the first A – B will give -10 ! state of its operand. If a condition is true, then logical NOT !(A && B) is true
* Multiplies both operands A * B will give 200 operator will make it false.
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 2
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by one A-- will give 9
Prepared by: Mr. Ted Elrico R. Tamparong
C.1.d Bitwise Operators C.1.e Assignment Operators
Operator Description Example
Bitwise operator works on bits and perform bit-by-bit operation. The truth Simple assignment operator, Assigns values from C = A + B will assign
tables for &, |, and ^ are as follows – =
right side operands to left side operand. value of A + B into C
p q p&q p|q p^q Add AND assignment operator, It adds right
C += A is equivalent to C
0 0 0 0 0 += operand to the left operand and assign the result to
=C+A
0 1 0 1 1 left operand.
1 1 1 1 0 Subtract AND assignment operator, It subtracts
C -= A is equivalent to C
1 0 0 1 1 -= right operand from the left operand and assign the
=C-A
Assume if A = 60; and B = 13; now in binary format they will be as follows −
result to left operand.
Multiply AND assignment operator, It multiplies
A = 0011 1100 C *= A is equivalent to C
B = 0000 1101 *= right operand with the left operand and assign the
=C*A
----------------- result to left operand.
A&B = 0000 1100
Divide AND assignment operator, It divides left
A|B = 0011 1101 C /= A is equivalent to C
/= operand with the right operand and assign the
A^B = 0011 0001 =C/A
~A = 1100 0011
result to left operand.
Operator Description Example Modulus AND assignment operator, It takes
C %= A is equivalent to
Binary AND Operator copies a bit to the result if it (A & B) will give 12 which %= modulus using two operands and assign the result
& C=C%A
exists in both operands. is 0000 1100 to left operand.
Binary OR Operator copies a bit if it exists in either (A | B) will give 61 which C <<= 2 is same as
| <<= Left shift AND assignment operator.
operand. is 0011 1101 C = C << 2
Binary XOR Operator copies the bit if it is set in (A ^ B) will give 49 which >>= Right shift AND assignment operator. C >>= 2 is same as
^ C = C >> 2
one operand but not both. is 0011 0001
(~A ) will give -61 which C &= 2 is same as
&= Bitwise AND assignment operator.
Binary Ones Complement Operator is unary and is 1100 0011 in 2's C=C&2
~
has the effect of 'flipping' bits. complement form due to C ^= 2 is same as
^= Bitwise exclusive OR and assignment operator.
a signed binary number. C=C^2
Binary Left Shift Operator. The left operands value C |= 2 is same as
A << 2 will give 240 |= Bitwise inclusive OR and assignment operator.
<< is moved left by the number of bits specified by the C=C|2
which is 1111 0000
right operand.
Binary Right Shift Operator. The left operands
A >> 2 will give 15 which
>> value is moved right by the number of bits specified
is 0000 1111
by the right operand.

Prepared by: Mr. Ted Elrico R. Tamparong


C.1.f Misc Operators
Operator Description Example D. Learning Activities/Exercises
sizeof operator returns the size of a int a; cout<<sizeof(a); Practice set.
sizeof()
variable. //outputs 4 in bytes Show the output of the following codes. (Assuming they are all in the int main() function).
Conditional operator (?). If Condition Code Output
int a = 3, b = 4; int a = 3, b = 4, c = 5, result;
Condition ? X : Y is true then it returns value of X
cout<< ( (a>b)?“Yes”: “No”); a++;
otherwise returns value of Y.
cout << “a * b = ” << (a*b) << endl;
Comma operator causes a b += c;
sequence of operations to be bool x,y,z,a,b,c=false; cout << “c + a = ” << (c + a) << endl;
performed. The value of the entire /*here only c is initialized as
,
comma expression is the value of false. The others are just
the last expression of the comma- plainly declared*/ float c = 13;
separated list. int z = 5;
Member operators are used to cout<<student.name; cout << “byte size of c: ” << sizeof(c) << endl;
. (dot) and -> cout << “byte size of z: ” << sizeof(z) << endl;
reference individual members of or
(arrow) cout << “Is size of z equal to c? : ” <<
classes, structures, and unions. cout << student ->name;
(sizeof(c)==sizeof(z)? “True” : “False”) << endl;
Casting operators convert one data char f = ‘a’;
Cast
type to another. cout << int(a);
int a = 4; bool x = false, y = true;
cout << &a;
Pointer operator & returns the
& /*will return the address of cout<<”True && False”<< ( x&&y? “True”:”False”)<<endl;
address of a variable. cout<<”True || False”<< ( x||y? “True”:”False”)<<endl;
variable a in your memory
(RAM)*/ cout<<”True && !False”<< ( x&&!y? “True”:”False”)<<endl;
int a = 4, *b;
Pointer operator * is pointer to a
* *b = &a;
variable.
//will assign b as a pointer to a
E. Teacher Intervention
C.2 Expressions For clarifications, feel free to chat, call, text (09069352363) or send an email
(tedelricot@gmail.com) message to me if you have questions, reactions, or reflections about the
An expression is a statement that results in a value. Hence, it is combination of
contents or activities in the lesson.
variables, operators, and operands which will be evaluated by the computer to result in a value.
Example:
int a = b+c*d; /* will first evaluate the right hand side then store the result to variable a
in the left hand side of the expression.*/

Prepared by: Mr. Ted Elrico R. Tamparong


VII. REFERENCES
1. Tutorials Point (I) Pvt. Ltd 2014. Learn C++ programming language. Available at
https://www.tutorialspoint.com/cplusplus/cpp_tutorial.pdf
2. Wallace Wang. 2004. Beginning Programming for Dummies 3rd Edition. Available at
https://www.pdfdrive.com/beginning-programming-for-dummies-e7970080.html. ISBN: 0-7645-
4997-9

Prepared by: Mr. Ted Elrico R. Tamparong


Prepared by: Mr. Ted Elrico R. Tamparong

You might also like