410 likes | 515 Views
C Programming. Class 10 Selection – Making Decisions. Relational, Equality, and Logical Operators. Relational less than < Operators greater than > less than or equal to <= greater than or equal to >=
E N D
C Programming Class 10 Selection – Making Decisions
Relational, Equality, and Logical Operators Relational less than < Operators greater than > less than or equal to <= greater than or equal to >= Equality equal to == Operators not equal to != Logical (unary) negation ! Operators logical and && logical or ||
Relational, Equality, and Logical Operators Relational less than < Operators greater than > less than or equal to <= greater than or equal to >= Equality equal to == Operators not equal to != Logical (unary) negation ! Operators logical and && logical or || The ! operator is unary. All the other relational, equality and logical operators are binary. They all operate on expressions and yield either the int value 0 or the int value 1.
Representing True or False in C • In the C language, false is represented by any zero value. • False can be represented by an int expression having a value of 0, a floating value having a value of 0.0, the null character ‘\0’, or the null pointer. • True is represented by any nonzero value.
Example of a Relational Operator • a < b • is a relational expression • If a is less than b, the expression has the value 1 • We consider the expression as being true • If a is not less than b, the expression has the value 0 • We consider the expression as being false
Common Errors UsingRelational Operators • a =< b /* out of order */ • a <= b /* correct */ • a < = b /* no space allowed between < and = */ • a <= b /* correct */
Values of Relational Expressions a - b a < b a > b a <= b a >= b positive 0 1 0 1 zero 0 0 1 1 negative 1 0 1 0 If a - b is positive, that means that a is greater than b. example: if a is 5 and b is 1. If a - b is zero, that means a is equal to b. example: if a is 5 and b is 5. If a - b is negative, that means a is less than b. example: if a is 1 and b is 5.
Precedence and Associativity with Relational Expressions Declarations and initializations int i = 1, j = 2, k = 3; double x = 5.5, y = 7.7; Expression Equivalent Expression Value i < j - k i < (j -k) 0 -i + 5 * j >= k + 1 ((-i) + (5 * j)) >= (k + 1) 1 x - y <= j - k - 1 (x - y) <= ((j - k) - 1) 1 x + k + 7 < y / k ((x + k) + 7) < (y / k) 0
Equality Operators & Expressions • The equality operators == and != are binary operators that act on expressions. Values of Equality Expressions a - b a == b a != b zero 1 0 nonzero 0 1
Precedence and Associativity with Equality Operators Declarations and initializations int i = 1, j = 2, k = 3; Equivalent Expression ExpressionValue i == j j == i 0 i != j j != i 1 i + j + k == -2 * -k ((i + j) + k) == ((-2) * (-k)) 1 ((1 + 2) + 3) == ((-2) * (-3)) 6 == 6
Logical Operators & Expressions • The logical operator ! is unary, and the logical operators && and || are binary. • When applied to expressions, each yields either the value 0 or the value 1.
Values of Negation Expressions a!a zero 1 nonzero 0
Evaluating Expressions with Logical Negation Declarations and Initializations int i = 7, j = 7; double x = 0.0, y = 999.9; ExpressionEquivalent ExpressionValue !(i - j) + 1 (!(i - j)) + 1 2 ! i - j + 1 ((!i) - j) + 1 -6 ! ! (x + 3.3) ! (! (x + 3.3)) 1 ! x * ! ! y (! x) * (!(!y)) 1
Values of Logical Expressions a b a && b a || b zero zero 0 0 zero nonzero 0 1 nonzero nonzero 1 1
a b a && b a || b F F F F F T F T T F F T T T T T Truth Table for && and ||
Precedence and Associativity of && and || • The precedence of && is higher than ||, but both operators are of lower precedence than all unary, arithmetic, and relational operators.
Evaluating Expressions with Logical && and || Operators Declarations and Initializations int i = 3, j = 3, k = 3; double x = 0.0, y = 2.3; Expression Equivalent Expression Value i && j && k (i && j) && k 1 x || i && j - 3 x || (i && (j - 3)) 0 i < j && x < y (i < j) && (x < y) 0 i < j || x < y (i < j) || (x < y) 1
Short Circuit Evaluation • With expressions that contain the operands && and ||, the evaluation process stops as soon as the outcome true or false is known. • Given the logical expression expr1 && expr2 • The evaluation stops if expr1 is determined to be zero (expr2 will not be evaluated).
Illustration of Short-Circuit Evaluation int i, j; i = 2 && (j = 2); /* what is printed */ printf(“%d %d\n”, i, j); /* 1 2 */ (i = 0) && (j = 3); printf(“%d %d\n”, i, j); /* 0 2 */ i = 0 || (j = 4); printf(“%d %d\n”, i, j); /* 1 4 */ (i = 2) || (j = 5); printf(“%d %d\n”, i, j); /* 2 4 */
Example of the Use of Short-Circuit Evaluation if (x >= 0.0 && sqrt(x) <= 7.7) { ..... /* do something */ If the value of x is negative, the square root of x will not be taken.
The if and if-else Statements • General Form of if statement. if (expr) statement • If expr is nonzero (true), then statement is executed. • Otherwisestatement is skipped and control passes to the next statement in the program.
Example of an if Statement if (grade >= 90) printf(“Congratulations!\n”); printf(“Your grade is %d.\n”, grade); /* The congratulatory statement */ /* is printed only if a grade of */ /* 90 or higher is earned. */
Nature of the Expression in an if Statement • Usually the expression is a relational, equality, or logical expression, but it can be any expression that evaluates to either zero or nonzero.
More Examples of if Statements if (y != 0.0) /* Avoid division by 0 */ x /= y; if (a < b && b < c) { /* check order */ d = a + b + c; printf(“Everything is in order.\n”); } if b == a /* parentheses missing */ area = a * a;
Grouping Statements Under a Single if • Two if statements if (j < k) min = j; if (j < k) printf(“j is smaller than k\n”); • A more efficient grouping if (j < k) { min = j; printf(“j is smaller than k\n”); }
The if-else Statement • General Form of if-else statement. if (expr) statement1 else statement2 • If expr is nonzero, then statement1 is executed and statement2 is skipped. • If expr is zero, then statement1 is skipped and statement2is executed.
Example of if-else Statement if (x < y) min = x; else min = y; printf(“Min value = %d\n”, min); /* Either x or y, whichever has */ /* the lesser value, has its */ /* value assigned to min. */
Another if-else Example if (c >= ‘a’ && c <= ‘z’) ++lc_cnt; else { ++other_cnt; printf(“%c is not a lowercase letter\n”, c); } /* The expression is the logical AND of two */ /* relational expressions. It has a value */ /* of 1 (true) if the value stored in the */ /* character variable c is a lowercase letter */
Common if and if-else Syntax Errors if b == a /* () missing */ area = a * a; if (a != b) { a += 1; b += 2; } ; /* ; caused syntax error */ else /* syntax error recognized */ c *= 3;
Embedding if Statements • The statement in an if statement can also be an if statement. if (a == 1) if (b == 2) printf(“***\n”);
The “Dangling else” Semantic Problem if (grade >= 80) if (grade < 90) printf(“Grade = B.\n”); else /* Wrong */ printf(“Grade = A.\n”); /*Indenting*/ ========================================= if (grade >= 80) if (grade < 90) printf(“Grade = B.\n”); else /* Correct */ printf(“Grade = A.\n”); /*Indenting*/
Solution to the “Dangling else” • An else attaches to the nearest if without an else. • That’s why if (grade >= 80) if (grade < 90) printf(“Grade = B.\n”); else printf(“Grade = A.\n”); is the correct formatting.
How About This One? if (grade >= 80) if (grade < 90) printf(“Grade is B.\n”); else printf(“Grade is less than B.\n”); Will it do what it is supposed to do? How can you keep both if statements and make the else go with the first if?
Another Use for the Empty Statement if (grade >= 80) if (grade < 90) printf(“Grade is B.\n”); else ; /* empty statement */ else printf(“Grade is less than B.\n”); Now it will work as intended.
/* Find the minimum of three values. */ #include <stdio.h> int main(void) { int x, y, z, min; printf(“Input three integers: “); scanf(“%d%d%d”, &x, &y, &z); if (x < y) min = x; else min = y; if (z < min) min = z; printf(“The minimum value is %d\n”, min); return 0; }
Nested Flow of Control • Flow-of-control statements such as if can be nested. • A common nested flow-of-control construct makes repeated use of if-else statements.
Nested if-else Statements Could be Written Like This: if (expr1) statement1 else if (expr2) statement2 else if (expr3) statement3 else if (expr4) statement4 else default statement next statement Except for next statement this is a single if-else statement. The else with the default statement is optional. The problem here is that the nested else-if statements march off the right side of the screen (or paper).
Alternate Style for else-if Statements This is the style you will see used and it is what you should use. if (expr1) statement1 else if (expr2) statement2 else if (expr3) statement3 else if (expr4) statement4 else default statement next statement
A Ternary Operator • The operators you have studied so far have been either unary or binary operators. Unary ExamplesBinary Examples -x a + b !y j && k • The conditional operator is a ternary operator.
The Conditional Operator • General Form expr1 ? expr2 :expr3 • First, expr1 is evaluated. If expr1 is nonzero (true), then expr2 is evaluated, and that is the value of the conditional expression as a whole. • If expr1 is zero (false), then expr3 is evaluated, and that is the value of the conditional expression as a whole.
Equivalent Code Using if-else or a Conditional Expression if(y < z) x = y; else x = z; x = (y < z)?y:z; The () are not necessary since the precedence of ?: is just above the assignment operators (and ?:associates from right to left).