Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Expressions

Similar presentations


Presentation on theme: "Conditional Expressions"— Presentation transcript:

1 Conditional Expressions

2 Control Statements A control statement will or will not be executed based on the value of the conditional expression scanf("%d", &pressureValue); while (pressureValue <= MAX_PRESSURE) { if (pressureValue == NOMINAL_VALUE) printf("Pressure is nominal\n"); else printf("Pressure is %d pounds\n", pressureValue); for (i = 5; i <= pressureValue; i = i + 10) printf("+"); printf("\n"); } // End while

3 Boolean Type A conditional expression evaluates to a value of true or false These are the two values for the Boolean type named after the mathematician George Boole When a conditional expression is evaluated, it results in a value of type Boolean In the C programming language there is no explicit Boolean type. Instead, false is 0 and true is any non-zero value. Many C programs use a #define macro to set a Boolean variable to true or false #define FALSE 0 #define TRUE 1 Conditional expressions are also called Boolean expressions A conditional expression is used whenever a true or false decision needs to be made about the value of certain variables or constants in a program

4 Expression Contents A conditional expression in C may contain any valid mathematical expression It may also contain relational operators and logical operators In addition, it may contain one or more function calls that return a value

5 Relational Operators ==,!= Equivalent, Not equivalent
<, <= Less than, Less than or equals >, >= Greater than, Greater than or equals if (A == B) C = 5; if (A != B) C = 10; for (i = 0; i < MAX_INDEX; i++) printf("*"); if (A <= B) D = 20; while (A > B) B++; if (A >= B) D = 40; A = B <= C; D = A == B;

6 Logical Operators && Logical AND || Logical OR ! Logical NOT
if ( (A == B) && (C <= D) ) E = 5; if ( (A != B) || (C >= D) ) F = 10; while ( !(A == B) ) B++; if ( !((A >= B) && (C <= D) || (E != F))) G = 100; A = B || C; D = A && B;

7 Boolean Algebra NOT false = true NOT true = false true AND true = true
true AND false = false true OR true = true true OR false = true false AND false = false false OR false = false if (0 || 0) printf("Does false OR false = true?"); if (0 && 0) printf("Does false AND false = true?");

8 More Examples ch1 = 'a'; ch2 = 'a';
printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1 ch1 = 'd'; ch2 = 'f'; ch2 = '\0'; printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 0

9 Example with Logical and Relational Operators
while ( (w <= x) && ( y >= z) ) { if ( (w < MAX_HEIGHT) || (y > MAX_DISTANCE) ) w = w + newHeight; y = y - newDistance; newValuesUsed = TRUE; } else w = w + oldHeight; y = y - oldDistance; newValuesUsed = FALSE; } // End if if ( !silentMode ) // Use of negative logic printf("Height: %d Distance: &d\n", w, y); } // End while

10 Operator Precedence Rule of thumb: Avoid complicated
Precedence refers to the order in which operations are evaluated in an expression a = w * x + !y || -u – v && b % c / d++; There is a hierarchy of rules that tells which operators are evaluated before other operators (i.e., unary before binary operators, multiplication before addition, relational operators before logical operators) The highest precedence goes to parentheses Rule of thumb: Avoid complicated expressions and use parentheses

11 Special Notes Avoid complicated conditional expressions, especially
When they are not a part of the original procedural design When they are created just to get the program to finally run Use meaningful names for Boolean variables and declare them of type int Ex. valueFound, errorStatus, moreData Do not compare Boolean variables to TRUE or FALSE if (valueFound == TRUE) X = 5; // Bad practice if (valueFound) X = 5; // Good practice if (valueFound == FALSE) X = 5; // Bad practice if (!valueFound) X = 5; // Good practice

12 Special Notes (continued)
Beware of the bitwise operators; they are not the same as the logical operators & bitwise AND | bitwise OR if (4 & 2) printf("Is this true?"); // Bitwise if (4 && 2) printf("Or is this true?"); // Logical Do not user == or != operator with values of type float double v = ; . . . if (v == ZERO) X = 10; // Bad if ((v >= ) && (v <= 0.001)) x = 10; // Good


Download ppt "Conditional Expressions"

Similar presentations


Ads by Google