


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive review of basic notions in c++ programming, focusing on variables, data types, and related concepts. Topics covered include what a variable is, variable types (int, double, char, bool), variable declaration, assignment, streams, and input/output operators. The document also discusses type compatibility, expressions, and unary operations.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what identifiers are legal? what are C-style? Pascal-style identifiers? what is variable type? what types have we studied? what is variable declaration? where (in the program) a variable declaration placed? what is an assignment? what is a stream? input stream? output stream? cout? cin? what is extraction/insertion operator? what is escape sequence? what is an input token?
type is the kind of data that is stored in variables int - whole numbers double - numbers with fractions since the storage is limited fraction can contain only a limited number of digits (usually up to 14) two ways to write a double number in C++ regular: 2.0 -3.23 +0.0456 .45 dot needs to be there scientific or floating point: 5.89e5 .045E-4 5e+5 dot does not need to be there mantissa is limited in size. The largest allowable number differs for every architecture. Usually: int - up to 32767 double - up to 10^308 int or double (or any other type in C++) cannot contain a comma other possible types are short, float and long double
char holds any single character declaration examples: char symbol, letter; character constants are put in single quotes: ‘A’ ‘+’ ‘a’ the uppercase and lower case of the same letter are different characters note the difference: strings placed in double quotes and can contain multiple characters: “this is a string” “A” single characters are stored in single quotes and can be stored in variables of type char: ‘A’ ‘Ab’ - wrong!
// asks for initials and outputs greeting #include
bool (short for boolean) is used for branching and looping statements a boolean variable can have only two values true or false bool result; result = true; true and false are boolean constants. They are keywords and their use is reserved this type is a fairly recent addition to C++ and old compilers may not support it
As a rule you cannot store a value of one type in a variable of another type trying to do it leads to type mismatch int intvar; intvar = 2.99; g++ prints this: warning: assignment to int' fromdouble' but still compiles the program discarding the fractional part it is usually a bad idea (but some programmers do it) to store char values in variables of type int, bools can also be in int. Even though you compiler allows it, it obscures the meaning of the variables and should be avoided
Expression is a mechanism of calculating new values of objects from old ones; expression is composed of operands and operations each expression has type as well as value simplest expression - constant with no operation applied; examples: 23 18.53 ‘a’
Operation Example addition + 2+3 a+4 ‘b’+ subtraction - count-2 4- multiplication * 56 widthheight division / 12/3 4/ remainder % 10%3 23% for positive integers: if the integer division is not even, then the fractional part of the result is discarded: 12/5 produces 2 note that the fractional part is discarded and the result is never rounded up: 11/3 which should be (3.6666…) produces 3 not 4 the remainder can be used to “catch” the “missing” fraction 12%5 produces 2
Operation Example addition + 2.3 + 3. subtraction - 2.45 - 1. multiplication * 5.4*2. division / 12.4 / 5. there is no remainder operations with floating point if there are integer and floating-point operands then the integers are first converted (by compiler) to floating-point operands and then the expression is evaluated: 45.34 * 2 is converted to 45.34 * 2.
Unary operations are allowed: +23 -2. precedence follows mathematical conventions:
When you divide int by int the result is int. It may be problematic in expression and the problem is hard to spot g++ would not complain this program converts feet into miles. Is there anything wrong with it? double total_price; int feet; cin >> feet; total_price = 5000 * (feet/5280);
// Compute Molecules from Mass #include