Вы находитесь на странице: 1из 3

1001

y 12.5 1002
1003
Variables Temperature 32
1004
1005
1006
Letter 'c' 1007
1008
Number - 1009
C++ Basics ? Variable can hold a number or a data of other types, it
always holds something. A variable has a name
? the data held in variable is called value
? variables are implemented as memory locations and
Variables, Identifiers, assigned certain memory address. The exact address
depends on computer and compiler.
Assignments, Input/Output
? We think as though the memory locations are actually
labeled with variable names

What are good identifers


Identifiers ? Careful selection of identifiers makes your program clearer
? Identifiers should be
? Name of a variable (or any other item you define in program) is ? Short enough to be reasonable to type (single word is norm)
called identifier – Standard abbreviations are fine (but only standard
? identifier must start with a letter or underscore symbol (_), the abbreviations)
rest of the characters should be letters, digits or underscores ? Long enough to be understandable
? the following are valid identifiers: ? Two styles of identifiers
x x1 x_1 _abc sum Rate averagE ? C-style - terse, use abbreviations and underscores to separate the
? the following are not legal identifiers. Why? words, never use capital letters for variables
13 3X %change data-1 my.identifier a(3) ? Pascal-style - if multiple words: capitalize, don’t use underscores
? C++ is case sensitive: ? Pick style and use consistently, I use C-style
MyVar and myvar are different identifiers ? Examples Pascal-style C-style
Min min
Temperature temperature
CameraAngle camera_angle
CurrentNumberPoints cur_point_nmbr

Keywords Keywords (cont.)


? Keywords are identifiers reserved as part of the language asm do if return typedef
auto double inline short typeid
int, return, float, double
bool dynamic_cast int signed typename
? They cannot be used by the programmer to name things break delete long sizeof union
? They consist of lowercase letters only case else mutable static unsigned
? They have special meaning to the compiler catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned
Variable Declarations
? Every variable in C++ program needs to be declared Where to declare
? declaration tells the compiler (and eventually the computer) what
kind of data is going to be stored in the variable
? the kind of data stored in variable is called it’s type ? The variables should be declared as close to the place where
? An object definition specifies they are used as possible.
Known List of one or
? Type ? If the variable will be used in several unrelated locations, declare
type more identifiers
? Name it at the beginning of the program:
? A common definition form: Type Id, Id, ..., Id; int main() {
<--- right here
? two commonly used types are:
? int - whole positive or negative numbers:
? note that variable contains a value after it is declared. The value
is usually arbitrary
1,2, -1,0,-288, etc.
? double - positive or negative numbers with fractional part:
1.75, -0.55
? example declarations:
int number_of_bars;
double weight, total_weight;

Output
To do input/output you have to insert
Assignment ?
#include <iostream>
Var = value; at the beginning of your program
? C++ uses streams for input an output
? Assignment statement is an order to the computer to set the ? stream - is a sequence of data to be read (input stream) or a sequence
value of the variable on the left hand side of the equation to what of data generated by the program to be output (output stream)
is written on the right hand side ? variable values as well as strings of text can be output to the screen
? it looks like a math equation but it is not ! using cout (console output):
? Example: cout << number_of_bars;
number_of_bars = 37; cout << “ candy bars”;
total_weight = one_weight; cout << endl;
total_weight = one_weight * number_of_bars; ? << is called insertion operator, it inserts data into the output stream,
number_of_bars = number_of_bars + 3; anything within double quotes will be output literally (without changes) -
“candy bars taste good”
? note the space before letter “ c”- the computer does not inert space on
its own
? keyword endl tells the computer to start the output from the next line

More Output Escape Sequences


? Certain sequences of symbols make special meaning to the
computer. They are called escape sequences
? The data can be stringed together: ? Escape sequence starts with a backslash (\). It is actually just one
cout << number_of_bars << “ candy bars\n” special character.
? symbol \n at the end of the string serves the same purpose as ? Useful escape sequences:
endl
– new-line \n
? Arithmetic expressions can be used with the output statement: – horisontal tab \t
cout << “The total cost is $” << (price + tax);
– alert \a
– backslash \\
– double quote \”
? What does this statement print?
cout << “\” this is a \t very cryptic \” statement
\\ \n”;
Input
? cin - (stands for Console INput) - is used to fill the values of
Escape Sequences (cont.) variables with the input from the user of the program
? when the program reaches the input statement it just pauses until
Character ASCII Name Sequence the user types something and presses <return>
? therefore it is beneficial to precede the input statement with some
newline NL \n explanatory output:
horizontal tab HT \t cout << “Enter the number of candy bars
cout << “and weight in ounces.\n”;
backspace BS \b
cout << “then press return\n”;
form feed FF \f
cin >> number_of_bars >> one_weight;
alert or bell BEL \a
? >> is called extraction operator
carriage return CR \r
? note how input statements (similar to output statements) can be
vertical tab VT \v
stacked
backslash \ \\ ? input tokens (numbers in our example) should be separated by (any
single quote ' \' amount of) whitespace (spaces, tabs, newlines)
double quote " \" ? the values typed are inserted into variables when <return> is
question mark ? \? pressed, if more values needed - program waits, if extra typed - they
are used in next input statements if needed

Вам также может понравиться