210 likes | 216 Views
Conditionals. Conditional Execution. A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression if the withdrawal is more than the bank balance, then print an error if today is my birthday, then add one to my age
E N D
Conditional Execution • A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression • if the withdrawal is more than the bank balance, then print an error • if today is my birthday, then add one to my age • if my grade is greater than 3.5, then give ‘A’ as final grade
Conditional statements in C • C supports several forms for conditionals: • if • if-else • if-elsif-elsif-…-else • And more… • You as a programmer must decide which form to use. • General semantics: the conditional statement depends on a boolean condition, the guard; if this condition is true the statement that depends on it will get executed;
Conditional Statement in C if (condition) statement; • The statement is executed if and only if the condition is true. • if (x < 100) x = x + 1; • if (withdrawalAmount > balance) printf( “NSF check.\n”); • if (temperature > 98.6) printf(“You have a fever.\n”);
Conditional statements in C if (condition) statement1; else statement2; Semantics: if condition is true, statement1 gets executed, otherwise statement2 gets executed. if (value >= 70) passed = passed + 1; else notPassed = notPassed + 1;
Conditional statements in C if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; : : else if (conditionK) statementK; else statement; The first condition that is true (from top to bottom) will make its statement execute. If all conditions are false last statement executes.
Conditional Example if (grade >= 90) countAs = countsAs + 1; else if (grade >= 80) countBs = countBs + 1; else if (grade >= 70) countCs = countCs + 1; else if (grade >= 60) countDs = countDs + 1; else countFs = countFs + 1;
Flow of execution x = y + 10; (s1) if ( x < 100) x = x + 1; (s2) y = y – 10; (s3) • S2 depend on the condition (X < 10) • S1 and S3 do not depend on the condition (x < 10) Executes s1 true x<100 executes s2 false Does not execute s2 Executes s3
Conditional expressions • Also called "logical" or "Boolean" expressions • Made up of variables, constants, arithmetic expressions, and the "relational operators": • Math symbols: < , ,> , ,= , • in C: < , <=, > , >= , == , != • Conditional expression examples: airTemperature > 0.0 98.6 <= body_temperature maritalStatus == ‘M’ divisor != 0
Value of conditional expressions • In C conditional expressions are evaluated to yield an integer. • If the resulting integer value is 1, it is equivalent to TRUE. • If the resulting integer value is 0 it is equivalent to FALSE. • An arithmetic relational operator (== <, <=, >, >=, !=) will yield 1 when the relation is true and 0 when false. • Any integer expression can be used as a conditional expression: • If its value is not zero, it is true • Otherwise it its false. • Thus we can write: if (value) equivalent to: if (value !=0) statement; statement;
Formation of complex conditional expressions • Boolean operators && stands for the and || stands for the or ! Stands for the negation operator Not • Examples (x >= 0) && (x <=10) (y< 0) || (y > 10) !(x == 0) ! ( (x>=0) && (x <=10))
Boolean Tables for &&, ||, ! • Be1 && be2 be1 || be1 1 1 1 1 1 1 1 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 !0 yields 1 !1 yields 0
DeMorgan’s Laws • ! ( be1 && be2) == (!be1) || (!2be) • ! ( be1 || be2) == (!be1) && (!2be)
Precedence rules of expressions with boolean operators • Boolean ! Has higher precedence than && • && has higher precedence than || • &&, and || are left associative. • !x && y equivalent to (!x) && y • y && z || u equivalent to (y && z) || u • z1 || z2 || z3 equivalent to (z1|| z2) || z3 • z1 && z2 && z3 equivalent to (z1&& z2) && z3 • ! Has higher precedence than <, >, <=, >= • &&, || have lower precedence than <, >, <=, >= • !x < y equivalent to (!x) < y • X && y < z equivalent to x && (y < z)
Warning • Notice that when we write: if (be) statement1; statement2; the boolean expression is only affecting statement1. Statement2 will always execute after the ifis executed. What if we need to perform more than one statement based on a condition?
Blocks • Use a block to form a group of statements, which will groups together statements so that they are treated as a single statement: { statement1; statement2; …. } • With this notion we can write more complex conditional statements.
Example If (withdrawal <= balance){ balance = balance - withdrawal; monthyCharge = monthlyCharge + 1.0; } Else { monthlyPenalty = monthlyPenalty + 25.0; } To note: A single statement ends with semicolon A block does not have semicolon after ‘}’ A block statement can have zero or more statements Its best to use block statements in conditionals, it helps you reduces the number of errors
Pitfalls with if’s • Using ‘=‘ instead of ‘==‘ • It will not yield a compilation error • And accompaning statement might or might not execute. • In any case, it is a BUG hard to find!!!!!!!!!!! if ( value = average) statement;
Pitfalls with if’s • Althought the following statement will not give a syntactic error it is incorrect if ( low <= value <= high) value = value * 2; • You should write it as: if (low <= value && value <= high) value = value * 2;
Pitfalls with if’s • the operator & is different from && • the operator | is different from || • & and | are not boolean operators!!!!! • if used by mistake, you will get no syntax errors, but program will most likely produce incorrect results.
Pitfalls with if’s • When using doubles do not compare them for equality (==) or inequality (!=) • When using either of these two operators with doubles you might get incorrect results due to accuracy of doubles! 30.0* (1.0/3.0) == 10.0 • May yield false! • 1 + ½. + 1/3. + ¼. +…..+ 1/n. Will most likely not be equal to 1/n. + 1/(n.-1) + … + ½. + 1 for a large value of n.