110 likes | 733 Views
Selection Selection allows you to choose between two or more alternatives. In C this means that the course of your executing program will depend on the result of an expression. true (any other value but zero) false (zero) expression Statement 2 Statement 1 Logical Flow Selection
E N D
Selection Selection allows you to choose between two or more alternatives. In C this means that the course of your executing program will depend on the result of an expression. true (any other value but zero) false (zero) expression Statement 2 Statement 1 Logical Flow
Selection Logical data in C - C recognizes zero as a false value and any other nonzero value is considered true. Logical Operators - logical operators form conditions or logical expressions. Thenot operator ( ! ) changes a true value (nonzero ) to false ( zero ) and a false value ( zero ) to true (one ).
Selection The andoperator ( && ) is a binary operator with four distinct possible combinations of values in its operands. The or operator ( || ) is a binary operator with four distinct combinations of values in its operands.
Selection Short-circuit evaluation - C will stop evaluation when it knows for sure what the final result will be. false && ( anything ) true || ( anything ) after the first operand is evaluated and found to be false and the operator is the and operator ( && ) the second operand will not be evaluated ( this could cause unexpected results if the second operand has side effects ) Relational Operators - Relational operators support logical relations. They are all binary operators that accept two operands and compare them. The result is logical data, that is, it is always a zero or one.
Selection • The if …. else Statement - An if…else statement is a composite statement used to make a decision between two alternatives. • Syntax: • if ( expression ) • statement 1 • else • statement 2 • The expression can be any C expression. After it has been evaluated, if its value is true (not zero ), statement 1 is executed: otherwise, statement2 is executed. It is impossible for both statements to be executed in the same evaluation. • Syntactical rules for if…else statements: • The expression must be enclosed in parentheses. • No semicolon ( ; ) is needed for an if..else statement. Statement 1 and statement 2 may have a semicolon as required by their types. • The expression can have a side effect. • Both the true and false statements can be any statement (even another if…else statement) or can be a null statement.
Selection • We can swap the position of statement 1 and statement2 if we use the complement of the original expressions. • if ( x > y) • printf( “ x is greater than y\n”) ; • else • printf(“ y is greater than x\n”) ; • An if…else with a compound statement • if ( x != y) • { • printf( “ x is not equal to y\n”) ; • x = y; • } • else • { • printf(“ x is equal to y\n”) ; • } The semicolons belong to the expression statements not to the if…else statement Curly brackets
Selection • A null else statement: • if ( x > 7 && x < 10) • { • printf( “ x is either 8 or 9\n”) ; • } else is null
Selection • Nested if statements - when an if…else is included within an if…else, it is known as a nested if. • if ( x <= y) • if ( x < y) • printf( “ %d < %d\n”, x ,y); • else • printf( “ %d == %d\n”, x ,y); • else • printf(“ %d > %d\n”, x ,y); • Dangling else problem - This problem is created when there is no matching else for every if. Simple rule: else is always paired with the most recent unpaired if • if ( x <= y) • if ( x < y) • printf( “ %d < %d\n”, x ,y); • else • printf( “ %d == %d\n”, x ,y); The compiler pairs this if and else!
Selection • Multiway selection - multiway selection chooses among several alternatives. There are two different ways to implement multiway selection in C. The first is by using the switchstatement. The other is a programming technique known as the else-if that provides a convenient style to nest if statements. • The else-if -There is no such C construct as the else-if. Rather, it is a style of coding that is used when you need a multiway selection based on a value that is not integral. • if ( score >= 90 ) • grade = ‘A’ ; • else if (score >= 80 ) • grade = ‘B’ ; • else if (score >= 70 ) • grade = ‘C’ ; • else if (score >= 60 ) • grade = ‘D’ ; • else • grade = ‘F’ ; • The else-if is used when: • The selection variable is not an integral and • The same variable is being tested in the expression
Selection • The switch Statement - Switch is a composite statement used to make a decision between many alternatives. The selection condition must be one of the C integral types. • Syntax: • switch ( expression ) • { • case constant-1 : statement; • statement; • case constant-2 : statement; • statement; • …… • statement; • case constant-3 : statement; • ……. • statement; • case constant-n : statement; • statement; • default : statement; • ……. • statement; • } /* end switch */
Selection • Syntactical rules for the switch statement: • There must be at least one case statement. • Each case expression is associated with a constant. • The case expression is followed by a colon ( : ) and then the statement with which it is associated. • There may be one or more statements for each case. The case label simply provides an entry point to start executing the code. • Default is executed whenever none of the previous case values matched the value in the switch expression. The default is optional. • When the statements associated with one case have been executed, the program flow continues with the statements for the next case unless a break statement is used. The break statement causes the program to jump out of the switch statement (goes to the closing brackets and continues with code following the switch