introduction to programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to programming PowerPoint Presentation
Download Presentation
Introduction to programming

Loading in 2 Seconds...

play fullscreen
1 / 9

Introduction to programming - PowerPoint PPT Presentation


  • 90 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to programming' - mare


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
constants vs variables
CISP 1010 - Computer Science IConstants 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
primitive data types in c
CISP 1010 - Computer Science IPrimitive 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
variable names in c
CISP 1010 - Computer Science IVariable 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++
assignment operator
CISP 1010 - Computer Science IAssignment 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
creating variables
CISP 1010 - Computer Science ICreating 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
binary arithmetic operators
CISP 1010 - Computer Science IBinary 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
expressions
CISP 1010 - Computer Science IExpressions
  • 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;
operator of precedence
CISP 1010 - Computer Science IOperator 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