Variables and Constants - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Variables and Constants

Description:

pauses a program, allows user to enter a number. cin Radius; ... Requires single-quotation marks. char ch; ch = A'; cout ch endl; Displays: A ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 25
Provided by: Ann9198
Category:

less

Transcript and Presenter's Notes

Title: Variables and Constants


1
Chapter 3
  • Variables and Constants

2
3.1 Using Variables
  • A named memory location
  • Stores a value
  • Defined by type and identifier
  • Kind
  • Name
  • double Radius

type
identifier
3
assignment
  • Give a variable a value
  • Equal sign ()
  • double Radius
  • double Radius 10
  • double Radius 12.3

?
10
12.3
4
assignment statement structure
  • variable name value
  • Area 3.14 10 10
  • Assignment Complete Review 1 and 2 on
  • p. 3-3.

5
3.2 Obtaining a Value from a User
  • cin console input stream (iostream library)
  • pauses a program, allows user to enter a number
  • cin gtgt Radius

get from operator extraction operator
6
3.3 Using Named Constants
  • const (type ) (an identifier) (value)
  • Represents a value that does NOT change
  • const double PI 3.14
  • cout ltlt Area ltlt (PI Radius Radius)

7
3.4 Choosing Identifiers
  • Must start with a letter.
  • Must contain only letters, digits, and the
    underscore (_).
  • Can be of any length.
  • Are case-sensitive.
  • Cannot be a keyword (C reserve word).
  • Quickly and clearly understandable.

8
3.5 Built-In Data Types
9
double
  • store positive or negative real numbers
  • floating number
  • double j
  • j 123456789.0
  • cout ltlt j ltlt endl
  • Displays 1.23457e08

10
int and long
  • Store positive and negative integers
  • Difference between two, range
  • int i
  • i 12.9
  • cout ltlt i ltlt endl
  • Displays 12

11
char
  • Stores a single character.
  • Requires single-quotation marks.
  • char ch
  • ch A
  • cout ltlt ch ltlt endl
  • Displays A

12
3.6 Variable Definitions
  • Can be defined in single statements.
  • Can define multiple variables of the same type in
    a single statement.
  • int x, y, z
  • char ch1, ch2

13
3.6 Variable Definition contd
  • Can also include assignment.
  • double x 6
  • char ch1 A ch2 B
  • int y 2x

14
3.7 Expressions and Operators
  • Multiplication (), addition (), subtraction
    (-), and division (/) (NOTE no standard operator
    for exponentiation in C)
  • promotion C promotes the value with the
    narrower range (the integer) to the type of the
    value with the wider range (the double).

15
Integer Division
  • truncates the decimal portion of the quotient to
    result in an integer
  • An expression involving only integers results
    in an integer
  • cout ltlt (25/4) // Displays 6

16
typecasting
  • Converts the type of one value into a real
    number.
  • cout ltlt (double(25)/4)
  • Displays 6.25 because C promotes the 25 to a
    double and then real number division is performed.

17
modulus division
  • Returns the remainder resulting from division.
  • cout ltlt (254) //Displays 1
  • 25/4 6R1
  • Useful in examing the digits of number, finding
    the number of minutes left over after hours have
    been account for.
  • May only be used with int or long values

18
operator precedence
  • Specific order of operations
  • 1 parenthesis
  • 2 multiplication, division, modulus
  • 3 addition, subtraction
  • Note operators of the same precedence are
    evaluated left to right.

19
3.8 The string Library
  • A sequence of characters, like a message or name.
  • include ltlvp\spring.hgt
  • Takes input to the first blank.

20
getline
  • Requires the first item in parentheses to be the
    name of the input stream
  • Second item, name of the string variable.
  • getline(cin, Name)

21
string variable assignment
  • Can be assigned values like other variables
  • Name Fedor
  • concatenation joining of two strings.
  • cout ltlt (FirstName LastName)

22
3.10 Output Formatting
  • Useful for lining up numbers.
  • Statement is required for each item output.
  • cout.width(10) cout ltlt 123 ltlt endl
  • cout.width(10) cout ltlt 12 ltlt endl
  • cout.width(10) cout ltlt 12345 ltlt endl

23
setf()
  • default output is right aligned.
  • cout.setf(iosleft)

24
precision()
  • cout.setf(iosfixed) // fix num of decimals
  • cout.precision(3) // set number of dec to 3
Write a Comment
User Comments (0)
About PowerShow.com