0% found this document useful (0 votes)
115 views5 pages

Lab 3 Variables, Constants and Operators: 3.1 Objectives

- The document discusses variables, constants, data types, input/output statements, arithmetic operators, expressions, and type casting in C++. - It defines variables and constants, lists common data types and their storage sizes, and explains that constants are defined with const while variables can change values. - Input is handled with cin statements and output with cout statements. Arithmetic operators like +, -, *, / and % are covered along with precedence. Expressions can combine variables, constants and operators. - Type casting is explained as a way to explicitly convert between data types, like casting an integer to a float.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views5 pages

Lab 3 Variables, Constants and Operators: 3.1 Objectives

- The document discusses variables, constants, data types, input/output statements, arithmetic operators, expressions, and type casting in C++. - It defines variables and constants, lists common data types and their storage sizes, and explains that constants are defined with const while variables can change values. - Input is handled with cin statements and output with cout statements. Arithmetic operators like +, -, *, / and % are covered along with precedence. Expressions can combine variables, constants and operators. - Type casting is explained as a way to explicitly convert between data types, like casting an integer to a float.

Uploaded by

hazyhazy9977
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB 3

VARIABLES, CONSTANTS AND OPERATORS


3.1 OBJECTIVES

- To get familiar with basic data types


- To learn the difference between constant and variable
- To get familiar with cin statement
- To learn the use of arithmetic operators in expression evaluation
- To learn and implement the concept of type casting

3.2 PRE-LAB READING

3.2.1 VARIABLES AND BASIC DATA TYPES

We know from the previous classes that a variable is such an information which keeps on changing all
the time. In C++, variables serve the same purpose. Upon declaration of variable in a program, a
sufficient place become reserved in computer memory. This memory space is labeled with a specific
name chosen by programmer. The following program is introducing a variable myAge and printing its
value on output screen.

#include <iostream>
using namepace std;
int main()
{
int myAge = 18;
cout<<"My age is “ <<myAge<<“years”;
return 0;
}

Here myAge is an identifier for a variable and line# 5 is assigning a value 18 to this variable. This equal
sign is serving as an assignment operator. In other words we are defining a variable in this line.

While introducing a variable in a program you have observed that we also need to tell which type of
variable is this. Type is always being written before variable’s name as in the example below:

int myAge;

This statement tells the compiler to reserve a space in memory, labeled as myAge. And how much space
will be allocated? This depends on the type of variable of course.
Data Type Storage Size in memory
int (whole numbers) 4 bytes
char (characters) 1 byte
float (decimal numbers) 4 bytes
double (decimal numbers) 8 bytes
Note: Always choose an appropriate data type for a variable after identifying the nature of the value
going to be stored in it.

3.2.2 DIFFERENCE BETWEEN CONSTANTS & VARIABLES


Components of memory in which data values stored can change during the execution of the program
are called “variables”. Components of memory in which data values stored are initialized once and never
changed during the execution of the program are called “constants”. For example in the following
program we have introduced a variable as well as a constant.

#include <iostream>
using namespace std;

const double PI = 3.14; //declaration of a constant

int main()
{
float radius; //declaration of a variable
radius = 4.0;
cout << "PI = " << PI << endl;
cout << "Radius = " << radius << endl;
cout << "Circumference = " << 2 * PI * radius << endl;
return 0;
}

It is very clear form above example that the way we define a constant is almost same as the way a C++
variable is being defined. The difference is that constants are preceded (in C++) by the word const. You
can give any identifier (i.e. name) to them. Here “PI” is an identifier for a constant, which is of type
double. The value assigned to this constant is 3.14, which will remain fixed throughout the execution of
program.

Note: It is good programming practice to use named constants in your program to explain the meanings
of numeric values.

Activity 1:
Try changing the value of a constant later. Does it work? Does it give an error?
IDENTIFIERS
Identifiers are used to name variables, constants and many other components of a program. There are
following rules which we must need to follow to write an identifier:
 They should consist exclusively of letters, digits and the underscore _ character
 They cannot begin with a digit
 They cannot duplicate reserved words used in C++ such as int or if
 All characters in C++ are case sensitive; thus memory locations called simple, Simple, and
SIMPLE are three distinct locations

3.2.3 INPUT STATEMENT


Just as the cout statement transfers data from the computer to the “outside” world, the cin statement
transfers data into the computer from the keyboard. For example:
int yourAge;
cout << ”Please enter your age: ”; //Display prompt

CIN >> YOURAGE;


The extraction operator >> extracts an item from the input stream. In this case, since yourAge is an
integer, this instruction will wait for an integer to be entered at the keyboard and then will place that
number in the memory location called yourAge.
When a program asks for user input, it should first print a message that tells the user which input is
expected. Such a message is called a prompt.

3.2.4 ARITHMETIC OPERATORS & EXPRESSION EVALUATION

The following are some arithmetic operators used to perform different operations on
variables/constants. Most of them are easy to understand and use.

Operator Symbol Operation


+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Integer Division

When we divide two integers, of course the result of this division will be an integer. This is because the
decimal part is truncated or “chopped” from the number. Thus 9/5 will give the answer 1 not 1.8. For
this reason there are two division operations for integer numbers. The modulus operator, (%) used only
with integers, gives the remainder of a division operation.
Therefore, 9 / 5 gives 1 while 9 % 5 gives 4 (the remainder of the division).
Activity 2:
Observe the output of following:

int numerator=9, denominator=7, Q,R;


Q = numerator/denominator;
R = numerator% denominator;
cout<<”I am dividing ”<<numerator<<” by ”<<denominator<<endl<<endl;
cout<<”Quotient of division is: ”<<Q<<endl;
cout<<”Remainder of division is: ”<<R<<endl;
Practice using the modulus operator a little until you are comfortable with it.

C++ EXPRESSIONS
We know that an assignment statement consists of two parts: a variable on the left and a value to be
assigned on the right. The right side may have an expression as well. An expression in C++ is a
combination of variables, constants and various operators. For example:
circumference = 2*PI*radius ;
diff = 105 - 78 ;
ans = 8 * 4/2 + 9 - 4/2 + 6 * (4+3);

Expressions are evaluated (i.e. simplified and converted to one value) and the result is placed in the
memory location corresponding to the variable on the left. Let’s consider the last expression. Can you
tell in which order it would be evaluated?
The answer depends on the precedence of the operators used in this expression.

Precedence of Arithmetic Operators

Parenthesis
High to low

Unary operators
Multiplication, division, modulus
Addition, subtraction

To perform some other basic operations like “square root” and “power” in C++ we have the following in-
built functions (using them requires including the “cmath” library):

 sqrt (number)
√9 would be written as sqrt(9)
 pow (number, exponent)
23 would be written as pow(2,3)

TYPE CASTING
If you store a floating point number (i.e. a real number) into an int variable, you can lose information in
two ways:
• The fractional part is lost.
• The magnitude may be too large.

Yet, sometimes you do want to convert a floating-point value into an integer value. If you are prepared
to lose the fractional part and you know that this particular floating point number is not larger than the
largest possible integer, then you can turn off the warning by using a cast. A cast is a conversion from
one type (such as double) to another type (such as int). It is not safe in general, but it can be done when
you know it to be safe in particular circumstances.

int cents = static_cast<int>(100 * price + 0.5);

Consider another example:


int num = 10;
int den = 50;
float result;
result = num / den;

You have very clear idea in this problem that the value stored in result would be 0. What if you need to
get exactly 0.20 in “result” variable? We can achieve this by type casting.
result = static_cast<float>(num)/den;

Activity 3:
Try the following cast and see what happens:

char my_character = static_cast<char> ( 65 )


cout<<my_character;

You might also like