1 / 9

Introduction to programming

Introduction to programming. C++. 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.”

mare
Download Presentation

Introduction to programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CISP 1010 - Computer Science I Introduction to programming C++

  2. 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

  3. 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

  4. 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++

  5. 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

  6. 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

  7. 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

  8. 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;

  9. 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

More Related