Variables
Variables
Course
for
Beginners
Variables
By eng.Manar ELsheref
We will cover these skills
• Variables definition
• Output /input commands and variables
Outlines
PAGE 2
X=10
Y=5
Find the sum In Math
X+y=10+5=15
PAGE 3
Variables in C++
• Allow storage of data internally for the program
• Allow storage of information from the user
• There are different types of variables that service different needs
• Examples:
–Variables that store texts
– Variables that store integers (positive & negative numbers)
–Variables that store decimals (floating point numbers)
In C++, there are different types of variables (defined with different keywords), for example:
int stores integers (whole numbers), without decimals, such as 123 or -123
double stores floating point numbers, with decimals, such as 19.99 or -19.99
char stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
string stores text, such as "Hello World". String values are surrounded by double quotes
• variable_type:
The type of a variable depends on the type of data
we want to store Declare
Create
• variable_name:
The name of a variable, and how we want to refer to variables
it in the rest of the program.
int myAge = 35;
cout << "I am " << myAge << " years old.";
Text
Add Variables Together
To add a variable to another variable, you can use the + operator:
and
Example variable
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
PAGE 8
Declare Many Variables
To declare more than one variable of the same type, use a comma-
separated list:
Example
int x = 5, y = 6, z = 50;
cout << x + y + z;
.
One Value to Multiple Variables
You can also assign the same value to multiple variables in one line:
Example
int x, y, z;
x = y = z = 50;
cout << x + y + z;
PAGE 9
When you do not want others (or yourself) to override existing variable
values,
use the const keyword (this will declare the variable as "constant",
which means unchangeable and read-only):
Example
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
Constants
you should always declare the variable as constant when you have
values that are unlikely to change:
Example
const int minutesPerHour = 60;
const float PI = 3.14;
PAGE 10
You have already learned that cout is used to output (print) values.
Now we will use cin to get user input.
cin is a predefined variable that reads data from the keyboard with
the extraction operator (>>).
C++
In the following example, the user can input a number stored in the variable x. User
Then we print the value of x:
Example: Input
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
PAGE 11
Example of the C++ user input to accept the integer input from the user
#include <iostream>
using namespace std;
int main() {
int age;
cout<< "Enter the age: ";
Exa
cin>> age; mpl
cout<< "\nThe age entered by the user is: e
"<< age;
cout<<endl;
return 0; }
PAGE 12
Creating a Simple Calculator
In this example, the user must input two numbers.
Then we print the sum by calculating (adding) the two
numbers:
Example
int x, y;
Calculater
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
PAGE 13
End of Session
Meet you
in
the Next Session PAGE 14