Download presentation
Presentation is loading. Please wait.
Published byAbner Russell Modified over 8 years ago
1
LESSON 3 Expressions, Statements, & Operators
2
STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null statement). All statements end with a semicolon Even the null statement, which is just the semicolon and nothing else.
3
BLOCK & COMPOUND STATEMENTS Block Any place you can put a single statement, you can put a compound statement A block Begins with an opening brace({) Ends with a closing brace (}). Although every statement in the block must end with a semicolon, the block itself does not end with a semicolon.
4
EXPRESSIONS Expression Anything that evaluates to a value. Expressions are said to return a value. All expressions are statement.
5
Operators Operator => requires an action to be taken. Arithmetic Operator Logical operator Relational operator
6
OperatorSymbolExample ArithmeticOperationResultant Addition+3.0 + 5.28.2 Subtraction-7.5 - 4.03.5 Multiplication*8.0 * 5.040.0 Division/9.0 / 4.0 9/4 2.25 2 Modulo division%9 % 41 Relational Equal to=5 = 7FALSE Less than<5 < 7TRUE Greater than>5 > 7FALSE Less than or equal to<=5<=7 6<=6 TRUE Greater than or equal to>=5 >= 7FALSE Equal to= 5 = = 7FALSE Not equal to!=5 != 7TRUE Used for conditional statement, e.g. selection & looping statements. Logical Not !NOT TRUEFALSE And&&TRUE AND TRUETRUE Or||TRUE OR FALSETRUE Used for joining few conditional statements together. Output/result is in Boolean type (True/False) Higher priority
7
NOT A ! A TF FT ANDOR ABA && BABA || B TTTTTT TFFTFT FTFFTT FFFFFF
8
Rules of (arithmetic) operator precedence Operator(s)Operation(s)Order of evaluation (precedence) ( )ParenthesesEvaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level”, they are evaluated left to right. *, /, or %Multiplication, Division, Modulus Evaluated second. If they are several, evaluated left to right. + or -Addition, SubtractionEvaluated last. If there are several, they are evaluated left to right.
9
Example
10
Compound Statements/ Operators Assignment operatorsExampleExpression += x+=yx = x +y - = x - =yx = x – y *= x *= yx = x * y / = x /= yx = x / y %= x %= yx = x % y
11
Increment and Decrement operators OperatorExpressionExplanation ++++aIncrement a by 1 then use the new value of a in the expression in which a resides. ++a++Use current value of a in the expression in which a resides, then increment a by 1. ----bDecrement a by 1 then use the new value of b in the expression in which b resides. --b--Use current value of b then increment b by 1.
12
PostFix Expression Postfix increment: a++ a = a + 1 Postfix decrement: a-- a = a – 1 Example: a = 10; a++; a = 10; a--; b = 5; a = 10; c = b + a++; b = 5; a = 10; c = b + a--; x = 10; z = x ++; z = x--;
13
Example
14
Prefix Expressions Prefix Expression Prefix expression: ++a a = a + 1 Prefix expression: --a a = a –1 Example: a = 10; ++a; a = 10; --a; b = 5; a = 10; c = b + ++a; b = 5; a = 10; c = b + --a; x = 10; z = ++x ; z = --x;
15
Example
16
ASCII Table
17
Example
18
Hierarchy of Operations Order of OperationsOperand Data TypeResultant Data Type ( ) Reorders the hierarchy; all operations are completed within the parentheses using the same hierarchy. 1. Functions Mathematical Operators 2. *, /, %Numeric 3. +, -Numeric Relational Operators 4. ==,, =, !=Numeric or String or Character Logical Logical Operators (nonzero is TRUE, zero is FALSE) 5. NOT, !Logical 6. AND, &&Logical 7. OR, ||Logical Assignment Operator 8. =Numeric or Character or Logical
19
Additional Exercise Assume that: x = 26 y = 4 z = 7 Decide if the returned value will be 1 (true) or 0 (false) x=6 == x%y*3-y/z !(x+y<z) Decide if the return result, and changes to each variables y*++z-x y*z+++x
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.