Selection-Structure.pdf
Document Details
Uploaded by Deleted User
Full Transcript
SELECTION STRUCTURE G E R RY R I T Z R. D E N S I N G Adapted from A First Book of ANSI C, Fourth Edition CC 112 / CC 112L TOPICS COVERED Relational operators Relational Expressions Logical Operations If, If – Else an...
SELECTION STRUCTURE G E R RY R I T Z R. D E N S I N G Adapted from A First Book of ANSI C, Fourth Edition CC 112 / CC 112L TOPICS COVERED Relational operators Relational Expressions Logical Operations If, If – Else and nested If –Else Switch statement Flow of control refers to the order in which a program’s statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: – Normal flow of control for all programs is sequential – Selection is used to select which statements are performed next based on a condition – Repetition is used to repeat a set of statements – Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function REL ATIONAL OPERATORS Relational operators are important for making decisions. They allow us to compare numeric and char values to determine if one is greater than, less than, equal to, or not equal to another. Relational operators have binary meaning which require two operands. Relational operators have left to right associativity. Left to right associativity means that when two operators of same precedence are adjacent, the left most operator is evaluated first. REL ATIONAL EXPRESSIONS Relational expressions are also known as conditions A relational expression evaluates to 1 (true) or 0 (false) – The expression 3 < 4 has a value of 1 – The expression 2.0 > 3.3 has a value of 0 – The value of hours > 0 depends on the value of hours For example, because the relationship 33.3 is always false, the expression has a value of 0. This can be verified using the statements printf(“The value of 33.3); which results in the display The value of 33.3 is 0 In addition to numerical operands, character data can also be compared using relational operators. For example, in the ASCII code the letter A is stored using a code having a lower numerical value than the letter B, the code for a B is lower in value than the code for a C, and so on. For character sets coded in this manner, the following expressions are evaluated as listed Expression Value Interpretation ‘A’ > ‘C’ 0 False ‘D’ = ‘M’ 0 False ‘B’ != ‘C’ 1 True Comparing letters is essential in alphabetizing names or using characters to select a particular choice in decision-making situations. LOGICAL OPERATIONS In addition to using simple relational expressions as conditions, more complex conditions can be created using the logical operations AND, OR, and NOT. These operations are represented by the symbols &&, ||, and !, respectively. AND OPERATOR When the AND operator, &&, is used with two simple expressions, the condition is true only if both expressions are true by themselves. Thus, the compound condition (age > 40) && (term < 10) is true (has a value of 1) only if age is greater than 40 and term is less than 10. Because relational operations have a higher precedence than logical operators, the parentheses in this logical expression could have been omitted. OR OPERATOR The logical OR operator, ||, is also applied between two expressions. When using the OR operator, the condition is satisfied if either one or both of the two expressions are true. Thus, the compound condition (age > 40) || (term < 10) is true if either age is greater than 40, term is less than 10, or both conditions are true. NOT OPERATOR The NOT operator, !, is used to change an expression to its opposite state; that is, if the expression has any nonzero value (true), !expression produces a zero value (false). For example, assuming the number 26 is stored in the variable age, the expression age> 40 has a value of zero (it is false), while the expression !(age>40) has a value of 1. Since the NOT operator is used with only one expression, it is a unary operator. The relational and logical operators have a hierarchy of execution similar to that of the arithmetic operators. Table 4.6 lists the precedence of these operators in relation to the other operators we have used. As with all expressions, parentheses can be used to alter the assigned operator priority and improve the readability of relational expressions. By evaluating the expressions within parentheses first, the following compound condition is evaluated as: (6 * 3 == 36 / 2) || (13 < 3* 3 + 4) && ! (6-2