Expressions: - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Expressions:

Description:

Expressions: comprised of variables, constants, and operators Reminder: Variables must have data type, can change Constants used to aid readability – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 34
Provided by: SamT154
Category:

less

Transcript and Presenter's Notes

Title: Expressions:


1
Expressions
  • comprised of variables, constants, and operators
  • Reminder
  • Variables must have data type, can change
  • Constants used to aid readability
  • const float PI 3.14159
  • area PI radius radius
  • Or to limit changes to code
  • const string UNIV_NAME"Towson University"
  • By default, constants are of type double, not
    float.

2
operators
  • (unary plus) rarely used
  • - (unary minus)
  • Addition
  • - Subtraction
  • Multiplication
  • / Division
  • floating-point division yields a floating-point
    result
  • integer division yields integer quotient
    (truncation)
  • Modulus
  • remainder of integer divide
  • Requires both integer operands
  • Division and Modulus by 0 is illegal!

3
Mixed expressions
  • Contains both integer and floating point numbers
  • promoted to highest type
  • floatNum1 20 // convert 20 to 20.0 - type
    coercion
  • floatNum2 floatnum1 intNum  // coercion to
    float
  • floatNum3 5 intNum1 // convert the result
    to float
  • demotion -gt truncation
  • intNum 5.5 // assign to intNum the value
    5(truncation)
  • intNum 7.8 / floatNum  // convert the result
    to int

4
Operator examples
  • x x5
  • income salary bribes
  • rocky -12
  • net gross - takeout
  • y x z 2
  • avg total/numStudents //mixing types yields
    errors
  • intNum 3/4 //0
  • floatNum 3/4 //0
  • floatNum 7/2 //3
  • floatNum 7.0/2.0 //3.5
  • intNum 10 2 //0
  • intNum 17 3 //2

5
Combined Assignment Operators
  • number number 5 ? number 5
  • number number 10 ? number 10
  • -
  • /

6
Increment and Decrement
  • Increment
  • x x 1
  • x
  • x
  • Decrement --
  • --x
  • x--

7
Increment and Decrement
  • Prefix x gt increment x before using it
  • Postfix x gt increment x after using value
  • Beware of using in expressions!
  • x 5
  • y 5
  • cout ltlt x ltlt y //outputs 6 and 5
  • // final value
    of x and y is 6

8
Precedence rules
  • "who goes first" when expressions have multiple
    operators
  • rules are similar to rules used in algebra
  • () parentheses will override the precedence rules
  • When operators have same precedence, operations
    are performed left to right

9
() -gt . (prefix) -- (prefix)
(postfix) -- (postfix) unary ! - (cast) sizeof
/
-
ltlt gtgt
lt lt gt gt
!





?
- / lt
10
Type casting
  • explicit conversion of a value from one data type
    to another.
  • type (variable) //C-like casting
  • (type) variable
  • static_castltDataTypegt(value)
  • EX
  • int sum 60
  • int count 80
  • float average (float) sum/count
  • float average sum/float (count)
  • average static_castltfloatgt (sum) / static_cast
    ltfloatgt (count)

11
Functions
  • self-contained unit of program code designed to
    accomplish a particular task
  • Subroutines, procedures, modules, functions
  • C uses functions
  • block of code used more than once
  • Less code
  • reduces redundancy
  • more modular
  • More manageable
  • Break up work among people

12
functions
  • black box
  • there must be one main() function
  • main first (top down)
  • Two types of functions
  • void
  • value-returning

13
include ltiostreamgt using namespace std int
Square(int n) // function prototype void
DispCube(int n,)   // function prototype int
main( )    // caller         int x, ans      
// var declaration                 cout ltlt
"Enter a number "         cin gtgt x        
cout ltlt "The square of  " ltlt x ltlt " is " ltlt
Square(x) ltlt endl DispCube(x, ans)
        return 0 //function definitions
int Square(int n) //value returning
function          return n n void
DispCube(int n)          cout ltlt "The cube
of  " ltlt x ltlt " is " ltlt x x x ltlt endl
14
Creating and using a simple function
  • include ltiostreamgtusing namespace std
  • void PrintLogo()int main()
    PrintLogo() //function call
  • cout ltlt "Reminder" ltlt endl cout ltlt
    "Company Meeting" cout ltlt "Thursday at
    900!!!!"ltlt endl PrintLogo() return
    0
  • / end main /void PrintLogo() cout
    ltlt "YOUR COMANY" cout ltlt
    endl / PrintLogo /

15
Functions
  • Function declaration type functionName (arg
    list)
  • Argument list (type name, type name,)
  • can have 0 args
  • Exa
  • void DoStuff (int x, float y, char c)
  • formal arguments arguments in the function
    definition
  • actual arguments arguments in a function call
    (can be a constant, a variable, or an expression)

16
Functions
  • Value-Returning
  • call occurs in an expression
  • y Cube(x) 18
  • function returns exactly one result
  • type of function determined by return
  • Void
  • functions that do not return a value
  • Begin name with a verb
  • void PrintAvg(int,int,int)

17
void Functions
  • include ltiostreamgt
  • using namespace std
  • void  DisplayMessage (int n)  // function
    prototype
  • int main( )
  •    DisplayMessage(5) // function call
    stand-alone statement    cout ltlt "Good Bye" ltlt
    endl    return 0
  • void  DisplayMessage (int n)   // function
    definition
  •    cout ltlt  "I have liked and used C for  "
       cout ltlt n ltlt " years" ltlt endl

18
Library Functions
  • arithmetic expressions i/o
  • include the appropriate header file(s).
  • Some standard library functions
  • ltcmathgt exp(x), log(x), pow(x, y), sqrt(x)
  • ltcstdlibgt abs(n), rand()
  • See Appendix F in your text book!
  • Example include ltiostreamgt include ltcmathgt
    // for sqrt( ) and fabs( )

19
include ltiostreamgt
  • istream cin
  • ostream cout
  • --------------------------------------------------
    ----------
  • cin gtgt variable gtgt variable
  • type of variable
  • char reads in one printable character except
    blank
  • int integer value
  • double reads in decimal number

20
istream member functions
  • cin.get(varChar)
  • stores next input character into varChar
  • cin.get(ch1)
  • cin.ignore(intExp, chExp)
  • ignore intExp characters or ignore input until
    chExp
  • cin.ignore(100,\n)
  • istreamVar.putBack(ch)
  • Put the last character extracted by the get
    function back into the input stream
  • cin.putback(ch)
  • ch istreamVar.peek()
  • Returns the next character from the input stram
    but does not remove it
  • ch cin.peek()

21
Input failure
  • Trying to read a letter into an int
  • cin gtgt int1 gtgt int2 // W 54
  • gt Input stream enters fail state
  • istreamVar.clear()// returns input stream to
    working state
  • use ignore afterwards to clear buffer
  • cin.clear()
  • cin.ignore(200,\n)

22
output
  • cout ltlt expression or manip.
  • ltlt expression or manip.
  • integers and strings
  • By default consecutive integers and strings
    values are output without spaces between them.
  • floating-Point Numbers
  • by default, large and small numbers lapse into E
    notation (large and small are
    system-dependent).
  • number of decimal places varies from output to
    output(again, system-dependent).
  • No decimal point is displayed for whole numbers.

23
Example
  • x 12
  • y 345
  • z 6789
  • cout ltlt "The results are " ltlt x ltlt y ltlt z
  • The output will be The results are 123456789
  • Insert blanks/spaces within a line
  • Corrected output statement
  • cout ltlt "The results are " ltlt x ltlt " " ltlt y ltlt "
    " ltlt z
  • The output will be The results are 12  345  6789

24
manipulators
  • setprecision(n) sets precision of floating
    point numbers
  • fixed displays floating point numbers in fixed
    notation
  • scientific displays floating point numbers in
    scientific notation
  • showpoint causes a decimal point and trailing
    zeros to be displayed
  • setw (n) establishes print field of n spaces
    (right justified)
  • flush clears output buffer
  • setfill(ch) fills unues columns with ch
  • left causes subsequent output to be left
    justified
  • right causes subsequent output to be right
    justified
  • Parameterized functions require iomanip

25
setw(n)
  • set field width to n
  • controls how many character positions the next
    output should occupy (works only with numbers and
    strings, not char data).
  • applies to next item only
  • decimal point included in field width
  • The next output will be right-justified-useful to
    align columns of output EX   
  • cout ltlt setw(5) ltlt answer        cout ltlt
    setw(10) ltlt number_1

26
setprecision(x)
  • specifies number of decimal places
  • Remains in effect for all subsequent output,
    until you change it with another call to
    setprecision
  • Use it in combination with fixed and showpoint
    EX 
  • cout ltlt fixed ltlt showpoint
  • cout ltlt setprecision(2) ltlt price

27
SUMMARY
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • cout ltlt showpoint ltlt fixed ltlt setprecision(x)
  • cout ltlt setw(n) ..

28
//showpoint demo include ltiostreamgt include
ltiomanipgt using namespace std int main()
double x 10.0 cout ltlt "before showpoint x
" ltlt x ltlt endl cout ltlt showpoint
cout ltlt "after showpoint x " ltlt x ltlt endl
return 0
before showpoint x 10 after
showpoint x 10.0000
29
//fixed demo include ltiostreamgt using namespace
std int main() double x 10.0
double y 957234563.82 cout ltlt
"before fixed x " ltlt x ltlt y " ltlt y ltlt
endl cout ltlt fixed cout ltlt "after
fixed x " ltlt x ltlt y " ltlt y ltlt endl
return 0
before fixed x
10.0000 y 9.57235e008 after fixed x
10.000000 y 957234563.820000
LONG NUMBER IN DECIMAL NOTATION
30
//setprecision demo include ltiostreamgt include
ltiomanipgt using namespace std int main()
double x 10.0 double y
957234563.82 double z -45.67 cout
ltlt "before setprecision(3)" ltlt endl cout ltlt
"x " ltlt x ltlt y " ltlt y ltlt z " ltlt z ltlt
endl cout ltlt setprecision(3) cout ltlt
"after setprecision(3)" ltlt endl cout ltlt "x
" ltlt x ltlt endl cout ltlt "x " ltlt x ltlt y
" ltlt y ltlt z " ltlt z ltlt endl return
0 before setprecision(3) x 10.000000 y
957234563.820000 z -45.670000 after
setprecision(3) x 10.000 y 957234563.820 z
-45.670
31
//setw(15) demo include ltiostreamgt include
ltiomanipgt using namespace std int main()
double x 10.0 double y
957234563.82 double z -45.67
cout ltlt "before setw(15)" ltlt endl cout ltlt
"x " ltlt x ltlt endl ltlt "y " ltlt y ltlt endl ltlt "z
" ltlt z ltlt endl cout ltlt "after setw(15)"
ltlt endl cout ltlt "x " ltlt setw(15) ltlt x ltlt
endl ltlt "y " ltlt setw(15) ltlt y
ltlt endl ltlt "z " ltlt setw(15) ltlt z ltlt endl ltlt
endl return 0 before setw(15) x
10.000 y 957234563.820 z -45.670 after
setw(15) x 10.000 padded
with 9 blanks y 957234563.820 padded
with 2 blanks z
-45.670 padded with 8 blanks
32
//setw(10) demo include ltiostreamgt include
ltiomanipgt using namespace std int main()
double x 10.0 double y
957234563.82 double z -45.67
cout ltlt "before setw(10)" ltlt endl cout ltlt
"x " ltlt x ltlt endl ltlt "y " ltlt y ltlt endl ltlt "z
" ltlt z ltlt endl cout ltlt "after setw(10)"
ltlt endl cout ltlt "x " ltlt setw(10) ltlt x ltlt
endl ltlt "y " ltlt setw(10) ltlt y
ltlt endl ltlt "z " ltlt setw(10) ltlt z ltlt endl ltlt
endl return 0 before setw(10) x
10.000 y 957234563.820 z -45.670 after
setw(10) x 10.000 padded with
blanks y 957234563.820 exceeds minimum
width z -45.670 padded with 3 blanks
33
Examples
  • ans 45
    xxxxxcout ltlt setw(4) ltlt ans ltlt endl 45cout
    ltlt setw(5) ltlt ans ltlt endl 45cout ltlt setw(4)
    ltlt "Hi" ltlt endl Hicout ltlt setw(3) ltlt "Help"
    ltlt endl Helpx 310.0
  • cout ltlt setw(10)
  • ltlt setprecision(2) ltlt x ----310.00
  • cout ltlt setw(10) ltlt setprecision(5) ltlt x
    -310.00000cout ltlt setw(7) ltlt
    setprecision(5) ltlt x 310.00000
Write a Comment
User Comments (0)
About PowerShow.com