100 likes | 239 Vues
This guide provides a comprehensive overview of foundational concepts in C++ programming, including the distinction between constants and variables, as well as primitive data types such as integers, floats, characters, and booleans. Learn the rules for naming variables, assignment operations, creating and initializing variables, and understanding binary arithmetic operators. The content also covers expression evaluations, operator precedence, and essential programming principles that serve as a foundation for more advanced concepts in computer science.
E N D
CISP 1010 - Computer Science I Introduction to programming C++
CISP 1010 - Computer Science I Constants vs. Variables • Constants • Numeric – a numeric value • Ex. 5, -7, 98.6 • Character – a single symbol • Ex. ‘A’, ‘a’, ‘?’, ‘ ‘, ‘5’ • String – a group of one or more characters • Ex. “Hello”, “98.6”, “Mary had a little lamb.” • Note: Usually referred to as a literal • Variables • Symbolic name for a memory location • Contents may change • May be of any supported data type • Ex: x, val, Age, userName
CISP 1010 - Computer Science I Primitive Data Types in C++ • Integer values • int– used for most integer values • short (aka short int) – used to hold “small” values • long (aka long int) – used when larger capacity is needed • may be positive or negative unless specified as unsigned • Floating point values • float– have a whole number and a fractional part • double – used when a higher degree of precision is needed • may be positive or negative unless specified as unsigned • Character values • char – hold a single letter, digit or symbol • Boolean values • bool – holds a true or false value
CISP 1010 - Computer Science I Variable Names in C++ • Generally can be any combination of alphabetic characters or digits or and underscore except: • Whitespace – no space, tab, return, etc. • Use capitalization or underscore to imply a space • myArray or first_number • No digits or special characters in first position • No operators such as + or – • May begin with underscore : _val • Names are case sensitive • Ex: age, Age, age1, userName, UserName are all unique • Can not be a C++ reserved word • Ex: int, float, cin, cout, sizeof all mean something to C++
CISP 1010 - Computer Science I Assignment Operator • To assign a value to a variable the = operator is used • age = 25; • number = 0; • temp = 98.6; • Note: each statement ends in a semi-colon • Assignments may use constants, variable or expressions • number = 7; // number now holds 7 rather than 0 • number = age; // number now holds 25 rather than 7 • number = number + 1; // number now holds 8
CISP 1010 - Computer Science I Creating Variables • Variables have to be declared (to the compiler) • intval; • float celsius; • double standard_deviation; • bool flag; • Multiple variables of the same type can be declared • int x, y, z; • char a, b, ch; • Variables can be initialized • float temp = 98.6; • char letter = ‘a’; • bool state = true; • intnum, val = 3; // note that only val is initialized to 3 here
CISP 1010 - Computer Science I Binary Arithmetic Operators • + used to add two values • x = y + 1; // x holds sum of y plus 1, y is unchanged • -used to subtract a value from another values • x = y – 1; // x holds difference, may be negative • * used to multiply a value times another • z = x * y; // z holds product, x & y unchanged • / used to divide a value by another • quotient = x / y; // hopefully quotient can hold a float • % used to extract the remainder after long division • z = x % 2; // z holds the remainder and will be an integer • performed modular arithmetic
CISP 1010 - Computer Science I Expressions • Expressions may include variables and constants • x + 1 • p * q • An assignment statement can have an expression on the right side only • y = x + 1; • z = x * y; • q = q * q; • p * q; // a complete statement with no assignment • Traditional algebra statements must be rewritten • a + b = c; // invalid • c = a + b; // valid • Multiple assignments are valid as long as they can be evaluated • a = b = c; or a = b = 1; or a = b = c + 1; but not a = b + 1 = c + 1;
CISP 1010 - Computer Science I Operator of Precedence • When multiple operators are used in the same expression there is a precedence that determines the order they are evaluted. • High order: *, /, % • Lower order: +, - • Operators of equal order are evaluated left to right • Assume: • intval, a = 3, b = 2, c= 5; • val = a + b * c; // mutiplication 1st so val is now 13 • val = a + b – c; // addition 1st so val is now 0 • val = a + 3 * b + 1; // val is now 10 • Highest order: ( ) can override precedence • val = ( a + 3 ) * ( b + 1 ); // val is now 18