Docsity

Understanding Conditional Expressions and Boolean Logic in C Programming, Slides of Computer Science

Partial preview of the text

Download Understanding Conditional Expressions and Boolean Logic in C Programming and more Slides Computer Science in PDF only on Docsity!

Conditional Expressions

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"); scanf("%d", &pressureValue); } // End while Docsity.com

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

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;

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

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'; printf("ch1 OR ch2 = %d\n", ch1 || ch2); // 1 printf("ch1 AND ch2 = %d\n", ch1 && ch2); // 1

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

Operator Precedence

  • 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

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