0% found this document useful (0 votes)
35 views20 pages

Lesson 2

The document discusses variables in C++. It defines a variable as a name that refers to a memory location where a value can be stored and changed. Variables allow programs to manipulate and store values. The document describes different types of variables like local and global variables. It also explains data types in C++, which specify the type of data a variable can hold, like int for integers, char for characters, and bool for Boolean values. The document concludes by discussing operators like relational operators to compare values, and arithmetic operators for mathematical operations.

Uploaded by

janueldefeo
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)
35 views20 pages

Lesson 2

The document discusses variables in C++. It defines a variable as a name that refers to a memory location where a value can be stored and changed. Variables allow programs to manipulate and store values. The document describes different types of variables like local and global variables. It also explains data types in C++, which specify the type of data a variable can hold, like int for integers, char for characters, and bool for Boolean values. The document concludes by discussing operators like relational operators to compare values, and arithmetic operators for mathematical operations.

Uploaded by

janueldefeo
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/ 20

Lesson 2

Variable
Declaration
What is Variable
A variable is simply a name the programmer uses to refer to the
computer storage locations. The term Variable are used because the value stored
in the variable can change, or vary,. The computer keep track of the actual
memory address that corresponds to each name the programmer assigned.

Variable and Constant are objects that a program manipulates. A


variable is a named of the memory location which stores a value. Variable can be
compared to empty containers. They can be filled by the user or programmer with
values.
int age;
it defines a variable with the name age which stores data of type int ( refers to an
integer). Variable are used to hold values. You can assign value to a variable like this example
below.

int age = 40;


The equal sign (=) represents an assignment statement, and its effect is to store the value of
40 in the memory location which goes by the name age.
Assignment Statement
A statement that stores the value of an expression into a variable

The syntax of assignment Statement

Variable = Expression

the semantic (meaning) of the assignment operator (=) is “store”; the value of the
expression is stored into the variable. Any previous value in the variable is destroyed and replace by
the value of the expression.
Expression
An arrangement of identifier, literals, and operators that can be evaluated to compute a
value of a given type.

Only one variable can be on the left-hand side of an assignment statement. An


assignment statement it not like a math equation (x+y=z+4); the expression (what is on the right
hand side of the assignment operator) is evaluated.
Evaluate
To compute a new value by performing a specified set of operations on the given values.

Examples
Given the declarations
string firstname;
int age;
string lastname;
the following assignment statements are valid:
Firstname =“Jacob Samuel”;
Age = 5;
Lastname = “Pomerada”;
Local Variables
Variables that are declared inside a function or block are local variables. They can be
used only by statements that are inside that function or block code.

Examples
#include <iostream>
using namespace std;
int main ()
{
//Local variable declaration
int a=0, b=0;
int product = 0;
//actual variable initialization by assigning values.
a = 5;
b = 10;
product = (a * b);
cout<<"The Product of: "<<a<<" and "<<b<<" is "<<product<<".";
return 0;
}
Global Variables
Global variables are defined outside of all the functions, usually on top of the program.
The global variables will hold their value throughout the lifetime of your program.

Examples
#include <iostream>
using namespace std;
//Global Variable declaration.
int sum;
int main ()
{
//Local variable declaration
int a, b;
//actual variable initialization by assigning values.
a = 5;
b = 10;
sum = (a * b);
cout<<"The sum of: "<<a<<" and "<<b<<" is "<<sum<<".";
return 0;
}
A program can have the same name for local and global variables but the value of a local
variable inside a function will take preference.

Examples

#include <iostream>
using namespace std;
//Global Variable declaration.
int a=100;
int main ()
{
//Local variable declaration
int a=50;
cout<<"The value is "<<a<<".";
return 0;
}
Data Type

This is a definition of a set of data that specifies the possible range of values of the set,
the operations that can be performed on the values, and the way in which the values are stored in
memory. Knowing the type of data enables the computer to manipulate it appropriately.
Data type in C++ are mainly divided
into two types:

1. Primitive data types : these data types are built-in or predefined data types and can be used
directly by the user to declare variables. Example: int, char, float, bool etc. Primitive data types
available in C++ are:

 integer
 Character
 Boolean
 Floating point
 Double Floating Point
 Valueless or Void
 Wide Character

Data Type Keyword Description

Boolean bool To hold a value true or false

Character char Storing a single character.

Integer int Positive and negative number


without a decimal point
Float float Number containing decimal
point and exponent
Double Float Point double Double precision floating point
number (high precesion)
Valueless of Void void Means without any value

Wide Character wchar_t Also now as character data


type
1. Integer – keyword used for integer data types is int. integers typically requires 4 bytes of
memory space and ranges from -2147483648 to 2147483647
2. Character – character data type is used for storing characters. Keyword used for character data
type is char. Characters typically require 1 byte of memory space and ranges from -128 to 127
or 0 to 225.
3. Boolean – Boolean data type is used for storing Boolean or logical values. A Boolean variable
can store either true or false. Keyword used for Boolean data type is bool.
4. Floating point – Floating point data type is used for storing single precision floating point
values or decimal values, keyword used for floating point data type is float.
5. Double floating point – Double floating point data type is used for storing double precision
floating point values or decimal values. Keyword used for double floating point data type is
double.
6. Void – Void means without any value. Void data type represents a valueless entity. Void data
type is used for that function which does not returns a value.
7. Wide Character – Wide character data type is also a character data type but this data type has
size greater than the normal 8-bit datatype.
Relational Operators
Relational operators refers to the construct or operator that tests or defines some
kind of relation between two entities.
Relational Operator Meaning

> Is greater than (comes after)

< Is less than (comes before)

== Is equal to

>= Is greater than or equal to (at least)

<= Is less than or equal to (no more


than)
!= Is not equal to
Relational Operators
example #include<iostream>
using namespace std;
int main ()
{
int age = 0;
cout<<"Legal Age Checker";
cout<<""<<endl;

cout<<"Give your age"<<endl;


cin>>age;

if (age>=18){
cout<<"\n\n";
cout<<"Your are already adult"<<endl;
}
else {
cout<<"\n\n";
cout<<"Sorry your are still a minor";
cout<<"\n\n";
}
return 0;
}
Arithmetic Operators
There are five arithmetical operations supported by C++ are:

Operators Description

+ Addition

- subtraction

* multiplication

/ division

% modulo
Arithmetic Operators
Operations of addition, subtraction, multiplication and division correspond
literally to their respective mathematical operations. The lest one is modulo
operator, represented by a percent sing (%), gives the remainder of a division of
two values. For example.

X = 11%3

Results in variable x containing the value 2, since dividing 11 by 3 results in 3,


with a remainder od 2.
#include<iostream>
Arithmetic Operators using namespace std;

int main ()
Example {
int divisor, dividend, qoutient, remainder;

cout<<"\n\n";
cout<<"\t Qoutient and Remainder Solver";
cout<<"\n\n";

cout<<"\t Give Dividend: ";


cin>>dividend;

cout<<"\t Give Divisor: ";


cin>>divisor;

qoutient = (dividend/divisor);
remainder = (dividend%divisor);
cout<<"\n\n";
cout<<"\t Qoutient is: "<<qoutient<<endl;
cout<<"\t Remainder is: "<<remainder<<endl<<endl;

cout<<"\t End of the Program";


return 0;
}
Logical Operator
Logical Operators used to combine two or more conditions/constraints or
complement the evaluation of the original condition in consideration. The result
of the operation of a logical operator is a Boolean value either true or false.
• Logical AND: the ‘&&’ operator returns true when both the conditions in
consideration are satisfied. Otherwise it returns false. For Example, a&&b
returns true when both a and b are true (i.e. non-zero)
• Logical OR: The ‘||’ operator returns true when one (or both) of the
conditions in consideration is satisfied. Otherwise it returns false. For
example, a || b returns true if one of a or b is true (i.e. non-zero). Of course,
it returns true when both a and b are true.
• Logical NOT: The ‘!’ operator returns true the condition in consideration is
not satisfied. Otherwise it returns false. For example, !a returns true if a is
false, i.e. when a=0
Example #include <iostream>
using namespace std;
int main()
{
int a =10, b=4, c=10, d=20;
//logical Operator
//Logical AND Example
cout<<"\n\n";
if (a>b&&c==d){
cout<<"\t A is greater than B AND C is qual to D.\n";
}
else{
cout<<"\t AND condition not statisfied.\n";
}

//Logical OR Example
if (a>b||c==d){
cout<<"\t A is greater than B OR C is equal to D.\n";
}
else{
cout<<"\t Neither A is greater than B OR C is equal to D. \n";
}

//logical NOT Example


if (!a){
cout<<"\t A is zero.\n";
}
else{
cout<<"\t A is not zero";
}
return 0;
}

You might also like