Title: Expressions and Statements
1Expressions and Statements
2Statements
- 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 /
3Whitespace
- 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
4Whitespace sometimes matters
- These are not equivalent
- Use common sense
OurWeight MyWeight YourWeight Our Weight
My Weight Your Weight
5Blocks 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
6Blocks and Compound Statements
- Blocks begin with the left brace and end with
the right brace - Blocks are not terminated with a semicolon
7Blocks and Compound Statements
- Blocks begin with the left brace and end with
the right brace - Blocks are not terminated with a semicolon
8Blocks and Compound Statements
- Blocks begin with the left brace and end with
the right brace - Blocks are not terminated with a semicolon
9Blocks and Compound Statements
- Blocks begin with the left brace and end with
the right brace - Blocks are not terminated with a semicolon
temp a
10Blocks 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
11Blocks 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
12Expressions
- 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
13Mathematical 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
14Combining the Assignment and Mathematical
Operators
int MyAge 5 int temp temp MyAge
2 MyAge temp MyAge MyAge 2 MyAge 2
15Increment and Decrement
C C 1 C 1 C C C - 1 C - 1 C- -
- There is no equivalent , //,
16Prefix and Postfix
int a, b, x 5 a x // x is 6, a is 5 b
x // x is 7, b is 7
17Precedence
- 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
18Nesting Parentheses
- It is ok to nest parentheses to clarify precedence
X a ((b c) / ((d - e) f))
19Truth
- 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
20The 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)
21The 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
22Indentation Styles
if (expression) statements if
(expression) statements if
(expression) statements
23Indentation Styles
if (expression) statements if
(expression) statements if
(expression) statements
24else
if (FirstNumber gt SecondNumber) cout ltlt
FirstNumber is bigger\n else cout ltlt
FirstNumber is not bigger\n
25Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
26Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
27Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
28Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
29Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
30Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
31Advanced if else Statements
if (expression1) if (expression2) statement1
else if (expression3) statement2 else
statement3 else statement4
32Logical Operators
AND expression1 expression2 OR expres
sion1 expression2 NOT ! !expression1
33Logical 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
34Logical 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
35Logical 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
36Conditional (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