C++ Basics: Variables, Assignments Input
C++ Basics: Variables, Assignments Input
Variables,
Assignments
Input
Main function
2
; statement terminator
Expression to the right of the operator is inserted (sent) to the cout object
Name
Declaration syntax:
Data type id, id, ..., id
Data Types
7
1.75, -0.55
float - positive or negative numbers with fractional part:
1.75, -0.55
char- alphabets
‘a’, ‘h’,’i’, etc
Example declarations:
int number;
double weight, totalWeight;
sizeof
8
var = expression;
Assignment statement is an order to the computer to set the
value of the variable on the left hand side of the equation
to what is written on the right hand side
Example:
number = 37;
totalWeight = oneWeight;
totalWeight = oneWeight * number;
number = number + 3;
Initialization
13
num=10;
num=20;
ans=num1+num2;
cout<<ans;
getch();
}
Named Constants
15
cin - (stands for Console Input) - is used to fill the variables with
the input from the user.
When the program reaches the input statement it just pauses until
the user types something and presses <Enter> key
cin >> num1;
cin >> num2;
>> is extraction operator
The values typed are inserted into variables when <Enter> is
pressed, if more values needed - program waits, if extra typed - they
are used in next input statements if needed
Simple Program
20
ans=num1+num2;
cout<<“The answer is”<<ans;//display statement
}