csci 130 chapter 4 statements, expressions, and operators
Post on 24-Dec-2015
231 Views
Preview:
TRANSCRIPT
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”);
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);
}
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)
Operators
• Instructs C to perform some operation
• Assignment =
• Mathematical
• Relational
• Logical
Unary Mathematical Operators
• Increment ++ increases value by 1• Decrement-- decreases value by 1• Ex:
x = 10; y = x++;
• Ex 2: x = 10; y = ++x;
Binary Mathematical Operators
• Addition +
• Subtraction -
• Multiplication *
• Division /
• Modulus %
• Ex:– int x = 100;
– int y = 9;
– z = x % y; (z holds the value 1)
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
Relational Operators
• Used to compare expressions
• Equal = =
• Greater than >
• Greater than or equal to >=
• Less than <
• Less than or equal to <=
• Not equal !=
Tip
• Do not confuse = (assignment) with = = (logical comparison of equality)
• Common errors: x = = z + 2; if (x = 3) ...
if statement
• General format 1: if (expression)
statement;
• General format 2: if (expression) {
statement 1;
statement 2;
….
Statement n;
}
Example if
scanf(“%f”, &salary)
if (salary > 0) { net = salary - (salary * tax); printf(“The net salary is %f”, net); }
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”);
Relational Expressions
• Relational expression evalute to:
0 (false)
1 (true)
• Ex:
x = (5 = = 5)
x holds the value 1
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
Precedence of Relational Operators
• Relational operators have lower precedence than mathematical operators
• if ((x + 2) > y) is the same as if (x + 2 > y)
Tip
Whenever possible, avoid the not operator Ex:
if (x != 5)
statement1;
else
statement2;
Is equivalent to: if (x == 5)
statement2;
else
statement1;
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
Logical Precedence
• NOT is evaluated before any math operators
• AND is evaluated after any math operators, but before OR
• OR is evaluated last
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
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
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
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
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
top related