Professional Documents
Culture Documents
COURSE/YEAR/SECTION INSTRUCTOR
Activity No. 4
Using Variables and Constants
When you allocate memory, you use a unique Try running the following code:
name to identify each memory location.
#include <iostream.h>
Additionally, you indicate the type of data to be #include <conio.h>
stored. A memory location whose value cannot void main()
be changed is called a named constant and has {
the following syntax: int i=10;
i=i+1;
const datatype identifier = value cout<<i<<endl;
getch();
Try running the following code: }
#include <iostream.h>
#include <conio.h>
void main()
{
const int i=5;
cout<<i<<endl;
What is the result/output? 11
Page 2 of 4
#include <string>
void main()
{
string str1;
str1="Hello World!";
cout<<str1<<endl;
getch();
}
Data that is received from the keyboard can be
put into variables using cin and the stream What is the result/output? Hello World!
extraction operator >>.
#include <iostream.h>
#include <conio.h>
void main()
{
char c;
cin>>c; Note: We have to use <string> in our program
getch(); code and not <string.h>.
}
Summary
What is the result/output? Data is stored onto main memory
In its current state, blank as this code will pause programmatically using either named constants
its execution, expecting the user to input a or variables. They must be unique throughout
character. Once a character is provided, the the program and can be manipulated using
program will conclude, and the outcome will be various operators.
determined by the specific character entered by
the user. Exercises
1. Evaluate the value of the final expression:
int a, b=9;
a=b;
y=4+d-x;
int number=10;
The data type string is a programmer-defined
data type. A string is a sequence of zero or more number=number+5;
characters enclosed in double quotation marks.
double val=10.0;
Every character has a relative position in the
string (including spaces). The length of a string val=val+0.5;
is the number of characters in it. C++ provides
const int b=9;
the library string to process strings
effectively. double def=123.5;
int y;
#include <iostream.h>
#include <conio.h>
Page 3 of 4
y=def;
int num1=8,num2=3,temp;
temp=num1;
num1=num2;
double x=5.0;
int y=4;
x=x+y/4;
double x=5.0;
int y=4;
y=x+y/4;
int count=0;
cin>>count;
Conclusion
References
https://www.w3schools.com/cpp/
cpp_variables.asp
https://www.learn-cpp.org/en/
Variables_and_Types