Constants and Variables - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Constants and Variables

Description:

Borland C provides a boolean variable type called bool and the constants true and false. Note: Some compilers, including Borland C version 4.2 or earlier do ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 19
Provided by: adria9
Category:

less

Transcript and Presenter's Notes

Title: Constants and Variables


1
Constants and Variables
2
Constants
  • The value of a constant cannot change throughout
    the entire program.

3
Literal Constants
  • Integer literal constants (e.g. 5, 643, -1003)
  • Floating point literal constants (e.g. 9.81, 0.5,
    .5, 3e8)
  • Characters are enclosed in single quotes (e.g.
    'M', 'd', '3', '')
  • Control characters are specified using a backward
    slash (e.g. tab '\t', newline '\n', Backward
    slash '\\')
  • Strings, which are a sequence of characters, are
    enclosed in double quotes (e.g. "Hello world ", "
    First line\nSecond line")

4
Named Constants
  • Provides a label to fixed values making programs
    easier to understand (avoids magical numbers)
    and easier to change (modify value in one place
    instead of many)
  • Syntax
  • const ltvariable typegt ltvariable namegt ltvaluegt
  • By convention constants have variables names that
    are all uppercase.
  • Example
  • const double SPEED_OF_LIGHT 2.997925e8
  • const double PI 2.0asin(1.0)

5
Variables
  • Variables are used to store and access dynamic
    values in memory. Instead of referring to memory
    contents by a physical memory address, variables
    provide an abstraction that simplifies
    programming.
  • Recall from the introduction where we introduced
    the task of adding two numbers. Instead of adding
    the value located at memory location 137 with the
    value located at memory location 138 and storing
    the results in memory location 139, it was much
    easier to think of c a b

6
Variables
  • Associated with variables are its
  • current value
  • location in computer memory (memory address)
  • data type (e.g. integer, real, character)
  • Note that the data type affects how much memory
    the variable uses as well as how the binary
    values stored in memory are interpreted.
  • Example 00110110 can represent the number 54 or
    the character 6

7
Variable Names
  • C is case sensitive
  • e.g. variable is a different variable than
    vAriable
  • Should describe the meaning of the variable
  • e.g. numberOfStudents, radiusOfCircle
  • Typically made up of letters, numbers, and
    underscores
  • e.g. weight1, slope_of_road
  • First letter cannot be a number
  • e.g. 3rdQuarter is invalid
  • Certain words are reserved by the compiler
  • e.g. for, if, return, int, float
  • Avoid using names that are the same as library
    functions
  • e.g. sin, cos, exp, sqrt

8
Variable Naming Conventions
  • Use lower case
  • e.g. salary instead of SALARY or Salary
  • Do not begin with an underscore
  • e.g. _result is not good
  • Do not use two underscores successively
  • e.g. final__score
  • Capitalize the first letter of all words except
    the first when not using underscores
  • e.g. numberOfStudents instead of numberofstudents

9
Variable Names
  • density
  • Time
  • tax-rate
  • main
  • ofStudents
  • pounds/inch
  • f(x)
  • finalexammark
  • integer
  • Final_Value
  • 1stTestMark
  • temperature
  • temperatureC
  • tethaRad

10
Variable Type
  • C supports several different data types
    including
  • integers whole numbers and their negatives
  • real fractional numbers
  • characters letters, symbols, digits
  • As stated before, different data types use
    different amounts of memory (which also may vary
    depending on the computer system)
  • Variable type Data type Memory size in bits
  • char character 8
  • short int integer 16
  • int integer typically 16 or 32
  • long int integer 32
  • float real 32
  • double real 64
  • long double real 80, 96, 128, sometimes larger

11
Boolean (True/False)
  • In C false is related to the value zero and
    true is related to any other value other than
    zero. When a boolean expression is evaluated in
    C, a true value will be represented by a value
    of one. Borland C provides a boolean variable
    type called bool and the constants true and
    false.
  • Note Some compilers, including Borland C
    version 4.2 or earlier do not have the bool
    variable type, and the constants true and false
    do not exist. In these cases you can define them
    yourself using the line
  • typedef enum false 0, true 1 bool
  • after your include directives.

12
Variable Declaration
  • Basic form
  • ltvariable typegt ltvariable namegt
  • List form
  • ltvariable typegt ltvariable 1 namegt, ltvariable 2
    namegt,
  • With initialization
  • ltvariable typegt ltvariable 1 namegt ltinitial
    valuegt, ltvariable 2 namegt ltinitial valuegt,

13
Average
  • Accept three user input values, prompting the
    user to enter each individual value, compute the
    average value of the three input values and
    display.
  • Pseudo code
  • Prompt user to enter value 1
  • Read in value 1
  • Prompt user to enter value 2
  • Read in value 2
  • Prompt user to enter value 3
  • Read in value 3
  • Compute the average value of the entered data
  • Display the average value

14
Average
Coding
alternative code (can group on one line) double
value1,value2, value3, averageValue
  • / header /
  • include ltiostreamgt
  • using namespace std
  • int main()
  • double value1
  • double value2
  • double value3
  • double averageValue
  • cout ltlt "Input value 1 "
  • cin gtgt value1
  • cout ltlt endl ltlt "Input value 2 "
  • cin gtgt value2
  • cout ltlt endl ltlt "Input value 3 "

strings are enclosed in double quotes
user input
end of line (carriage return)
end of line (carriage return)
15
Average
Program output
  • Input value 1 -1
  • Input value 2 32
  • Input value 3 68.3
  • The average value is 33.1

16
Assignment Statements
Stepping through the code
  • / header /
  • include ltiostreamgt
  • using namespace std
  • int main()
  • double value1
  • double value2
  • double value3
  • double averageValue
  • cout ltlt "Input value 1 "
  • cin gtgt value1
  • cout ltlt endl ltlt "Input value 2 "
  • cin gtgt value2
  • cout ltlt endl ltlt "Input value 3 "

value1 ? value2 ? value3
? averageValue ?
value1 -1 value2 ? value3
? averageValue ?
value1 -1 value2 32 value3
? averageValue ?
17
Assignment Statements
Stepping through the code
  • / header /
  • include ltiostreamgt
  • using namespace std
  • int main()
  • double value1
  • double value2
  • double value3
  • double averageValue
  • cout ltlt "Input value 1 "
  • cin gtgt value1
  • cout ltlt endl ltlt "Input value 2 "
  • cin gtgt value2
  • cout ltlt endl ltlt "Input value 3 "

value1 -1 value2 32 value3
68.3 averageValue ?
value1 -1 value2 32 value3
68.3 averageValue 33.1
18
cin
  • Two code fragments that have the same effect

double a, b, c cout ltlt "Enter the sides of a
triangle " cin gtgt a cin gtgt b cin gtgt c
double a, b, c cout ltlt "Enter the sides of a
triangle " cin gtgt a gtgt b gtgt c
Three ways to enter data values with the same
effect
Enter the sides of a triangle 17.3 12.9 9.1
Each of these three approaches is a stream of
input data containing the same three numbers
separated by blanks, tabs, and/or newline
characters (all of which are ignored when
extracting the numerical values from the input
stream
Enter the sides of a triangle 17.3 12.9 9.1
Enter the sides of a triangle 17.3 12.9 9.1
Write a Comment
User Comments (0)
About PowerShow.com