Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 130 Chapter 4 Statements, Expressions, and Operators.

Similar presentations


Presentation on theme: "CSCI 130 Chapter 4 Statements, Expressions, and Operators."— Presentation transcript:

1 CSCI 130 Chapter 4 Statements, Expressions, and Operators

2 Statements Complete direction to carry out single task Usually one per line Whitespace ignored except in strings Ex: –x = a + b; –printf(“Hello World”);

3 Code Block 2 or more C statements enclosed in braces Allowed anywhere a single statement is allowed Usually only used where necessary Use indentation for readablility Ex: for (int x = 0; x < 5; x++) { printf(“The value of x is “); printf(“%d”, x); }

4 Expressions Anything whose evaluation yields a numeric value –PI(symbolic expression defined in program) –20literal constant –ratea variable –700 / 63 - 42 –x = a + 10 –x = 6 + (y = 4 + 5)

5 Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical

6 Unary Mathematical Operators Increment++increases value by 1 Decrement--decreases value by 1 Ex: x = 10; y = x++; Ex 2: x = 10; y = ++x;

7 Binary Mathematical Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Ex: –int x = 100; –int y = 9; –z = x % y;(z holds the value 1)

8 Precedence 1. Parenthesis 2. Multiplication, division, modulus 3. Addition and subtraction Parenthesis can be used to give priority Ex: 3 * 7 - 4 + (17 +1) *.5 - 17 Result = 9

9 Relational Operators Used to compare expressions Equal= = Greater than> Greater than or equal to>= Less than< Less than or equal to<= Not equal!=

10 Tip Do not confuse = (assignment) with = = (logical comparison of equality) Common errors: x = = z + 2; if (x = 3)...

11 if statement General format 1: if (expression) statement; General format 2: if (expression) { statement 1; statement 2; …. Statement n; }

12 Example if scanf(“%f”, &salary) if (salary > 0) { net = salary - (salary * tax); printf(“The net salary is %f”, net); }

13 Sample if with else scanf(“%f”, &salary) if (salary > 0) { net = salary - (salary * tax); printf(“The net salary is %f”, net); } else printf(“Incorrect input”);

14 Relational Expressions Relational expression evalute to: 0 (false) 1 (true) Ex: x = (5 = = 5) x holds the value 1

15 Example x = (12 < 62); printf(“%d”, x); x = (5 != 3); printf(“ %d”, x); x = (12 < 62) + (5 != 3) + (5 < 3); printf(“ %d”, x); Output is as follows: 1 1 2

16 Precedence of Relational Operators Relational operators have lower precedence than mathematical operators if ((x + 2) > y) is the same as if (x + 2 > y)

17 Tip Whenever possible, avoid the not operator Ex: if (x != 5) statement1; else statement2; Is equivalent to: if (x == 5) statement2; else statement1;

18 Logical Operators AND&&if (exp1 && exp2) –True if both exp1 and exp2 are true OR||if (exp1 || exp2) –True if either exp1 or exp2 is true NOT!if (!exp1) –True if exp1 is false

19 Logical Precedence NOT is evaluated before any math operators AND is evaluated after any math operators, but before OR OR is evaluated last

20 Logical Operator Examples (3 != 5) || (6 < 8)true (3 != 5) || (6 > 8) && (9 = = 3)true ((3 != 5) || (6 > 8)) && (9 = = 3)false ((3 != 5) || (6 > 8)) && !(9 = = 3)true 3true 0false !3false

21 Compound Assignment Operators Shorthand method to combine assignment and binary math operations General form: –exp1 op= exp2 Examples: –x *= yis equivalent tox = x * y –x -= 6 + zis equivalent to x = x - 6 + z –y % =3is equivalent to y = y % 3

22 Conditional Operator The only C ternary operator (3 operands) General form: exp1 ? exp2 : exp3; exp1 is true - entire expression evaluates as exp2 exp1 is false - entire expression evaluates as exp3

23 Conditional Example x = (z < 36) ? 0: 1; –If z < 36, x is set to 0, otherwise 1 z = (x < y) ? x : y; –sets z equal to the smaller of x and y

24 The Comma Operator An expression can be formed by separating two expressions with a comma: –Both expressions evaluated (left first) –Entire expression evaluates to right expression Ex: –x = (a++, b++) –If x = 2, a = 3, b = 4 before the expression, –x = 4, a = 4, b = 5 after the expression


Download ppt "CSCI 130 Chapter 4 Statements, Expressions, and Operators."

Similar presentations


Ads by Google