24
CSCI 130 Chapter 4 Statements, Expressions, and Operators

CSCI 130 Chapter 4 Statements, Expressions, and Operators

Embed Size (px)

Citation preview

Page 1: CSCI 130 Chapter 4 Statements, Expressions, and Operators

CSCI 130

Chapter 4

Statements, Expressions, and Operators

Page 2: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Statements

• Complete direction to carry out single task• Usually one per line• Whitespace ignored except in strings• Ex:

– x = a + b;– x = a + b;– printf(“Hello World”);– printf(“Hello World”);

Page 3: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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);

}

Page 4: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Expressions

• Anything whose evaluation yields a numeric value– PI (symbolic expression defined in

program)– 20 literal constant– rate a variable– 700 / 63 - 42– x = a + 10– x = 6 + (y = 4 + 5)

Page 5: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Operators

• Instructs C to perform some operation

• Assignment =

• Mathematical

• Relational

• Logical

Page 6: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Unary Mathematical Operators

• Increment ++ increases value by 1• Decrement-- decreases value by 1• Ex:

x = 10; y = x++;

• Ex 2: x = 10; y = ++x;

Page 7: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Binary Mathematical Operators

• Addition +

• Subtraction -

• Multiplication *

• Division /

• Modulus %

• Ex:– int x = 100;

– int y = 9;

– z = x % y; (z holds the value 1)

Page 8: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

Page 9: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Relational Operators

• Used to compare expressions

• Equal = =

• Greater than >

• Greater than or equal to >=

• Less than <

• Less than or equal to <=

• Not equal !=

Page 10: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Tip

• Do not confuse = (assignment) with = = (logical comparison of equality)

• Common errors: x = = z + 2; if (x = 3) ...

Page 11: CSCI 130 Chapter 4 Statements, Expressions, and Operators

if statement

• General format 1: if (expression)

statement;

• General format 2: if (expression) {

statement 1;

statement 2;

….

Statement n;

}

Page 12: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Example if

scanf(“%f”, &salary)

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

Page 13: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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”);

Page 14: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Relational Expressions

• Relational expression evalute to:

0 (false)

1 (true)

• Ex:

x = (5 = = 5)

x holds the value 1

Page 15: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

Page 16: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Precedence of Relational Operators

• Relational operators have lower precedence than mathematical operators

• if ((x + 2) > y) is the same as if (x + 2 > y)

Page 17: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Tip

Whenever possible, avoid the not operator Ex:

if (x != 5)

statement1;

else

statement2;

Is equivalent to: if (x == 5)

statement2;

else

statement1;

Page 18: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

Page 19: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Logical Precedence

• NOT is evaluated before any math operators

• AND is evaluated after any math operators, but before OR

• OR is evaluated last

Page 20: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

3 true 0 false !3 false

Page 21: CSCI 130 Chapter 4 Statements, Expressions, and Operators

Compound Assignment Operators

• Shorthand method to combine assignment and binary math operations

• General form:– exp1 op= exp2

• Examples:– x *= y is equivalent to x = x * y

– x -= 6 + z is equivalent to x = x - 6 + z

– y % =3 is equivalent to y = y % 3

Page 22: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

Page 23: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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

Page 24: CSCI 130 Chapter 4 Statements, Expressions, and Operators

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