Expressions and Statements - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Expressions and Statements

Description:

White spaces are tabs, spaces, and newlines. White spaces are generally ignored ... Conditional (Ternary) Operator: ? : expression1 ? expression2 : expression3 ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 37
Provided by: tom8
Category:

less

Transcript and Presenter's Notes

Title: Expressions and Statements


1
Expressions and Statements

2
Statements
  • Statements end with the semicolon

x a b / this adds the values of a and b
and assigns the result to x / / this is
the null statement, it does nothing /
3
Whitespace
  • White spaces are tabs, spaces, and newlines
  • White spaces are generally ignored
  • These are all equivalent,
  • but some are easier to read than others.

xab x a b x a b
4
Whitespace sometimes matters
  • These are not equivalent
  • Use common sense

OurWeight MyWeight YourWeight Our Weight
My Weight Your Weight
5
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon

temp a a b b temp
6
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon

7
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon


8
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon


9
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon

temp a
10
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon

temp a a b
11
Blocks and Compound Statements
  • Blocks begin with the left brace and end with
    the right brace
  • Blocks are not terminated with a semicolon

temp a a b b temp
12
Expressions
  • Anything that evaluates to a value is an
    expression.

3.2 // returns the value 3.2 PI // float
const that returns the value 3.14 SecondsPerMinute
// int const returns 60 x a b // returns x
a b y x a b // assigns the return
value to y // a statement, but not an
expression
13
Mathematical Operators - /
  • Addition (), subtraction (-), multiplication
    (), division (/), modulus ()

x 5 2 // x is 7 x 5 - 2 // x is 3 x
5 2 // x is 10 x 5 / 2 // x is 2 x 5
2 // x is 1 x (double) 5 / 2 // x is 2.5
14
Combining the Assignment and Mathematical
Operators
int MyAge 5 int temp temp MyAge
2 MyAge temp MyAge MyAge 2 MyAge 2
  • also -, /, ,

15
Increment and Decrement
C C 1 C 1 C C C - 1 C - 1 C- -
  • There is no equivalent , //,

16
Prefix and Postfix
int a, b, x 5 a x // x is 6, a is 5 b
x // x is 7, b is 7
17
Precedence
  • See Operator Precedence, Appendix A

x 5 3 8 // x is 64, or x is 29? x (5
3) 8 // x is 64 x 5 (3 8) // x is
29 x 5 3 8 // is higher precedence
than // thus x is 29
18
Nesting Parentheses
  • It is ok to nest parentheses to clarify precedence

X a ((b c) / ((d - e) f))
19
Truth
  • 0 is false, false is zero
  • all other values are true, true is not zero
  • Relational operators evaluate as true (1) or
    false (0) , !, gt, gt, lt, lt

20
The Relational Operators
Equals 1 1 (true) 1 2
(false) Not Equals ! 1 ! 1 (false) 1
! 2 (true) Greater Than gt 1 gt 1 (false) 1
gt 2 (false) 2 gt 1 (true) Greater Than gt 1
gt 1 (true) 1 gt 2 (false) or Equals 2 gt 1
(true) Less Than lt 1 lt 1 (false) 1 lt 2
(true) 2 lt 1 (false) Less Than lt 1 lt
1 (true) 1 lt 2 (true) or Equals 2 lt 1 (false)
21
The if StatementControls Program Flow
  • If the expression is true, the following
    statement is executed.
  • If the expression is false, the following
    statement is not executed.

if (BigNumber gt SmallNumber) BigNumber
SmallNumber cout ltlt BigNumber ltlt BigNumber
ltlt endl cout ltlt SmallNumber ltlt SmallNumber
ltlt endl
22
Indentation Styles
if (expression) statements if
(expression) statements if
(expression) statements
23
Indentation Styles
if (expression) statements if
(expression) statements if
(expression) statements
24
else
if (FirstNumber gt SecondNumber) cout ltlt
FirstNumber is bigger\n else cout ltlt
FirstNumber is not bigger\n
25
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
26
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
27
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
28
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
29
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
30
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
31
Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
32
Logical Operators
AND expression1 expression2 OR expres
sion1 expression2 NOT ! !expression1
33
Logical AND
if ((x 5) (y 5))
  • expression1 expression2
  • if both expressions are true, then the logical
    AND statement is true
  • if either expression is false, then the logical
    AND statement is false
  • if both expressions are false, then the logical
    AND statement is false

34
Logical OR
if ((x 5) (y 5))
  • expression1 expression2
  • if either expression is true, then the logical
    OR statement is true
  • if both expressions are true, then the logical OR
    statement is true
  • if both expressions are false, then the logical
    OR statement is false

35
Logical NOT
if (!(x 5))
  • !expression
  • if the expression is true, then the logical NOT
    statement is false
  • if the expression is false, then the logical NOT
    statement is true

x !0 // the value of x is 1 x !1 // the
value of x is 0
36
Conditional (Ternary) Operator ?
z (x gt y) ? x y
  • expression1 ? expression2 expression3
  • if expression1 is true, return the value of
    expression2
  • if expression1 is false, return the value of
    expression3
Write a Comment
User Comments (0)
About PowerShow.com