Chapter 5 Selection Statement Selection Statement Definition A
- Slides: 20
Chapter 5: Selection Statement
Selection Statement: Definition § A selection statement causes the program control to be transferred to a specific flow based upon whether a certain condition is true or not.
Logical Expression § if statement must test the value of an expression § Relation operator is used along with if § A=10; B=20;
The if statement § If statement allows a program to choose two alternatives by testing the value if a expression § Syntax: if (expression) statement; Eg: a=10; b=20; if(a>b) printf(“A is greater than b”); if(b>a) printf(“ B is greater than a”); if(a==b) printf(“A and B are equal”);
The if statement contd… § For compound statement used braces § Syntax: if (expression) { statement 1; Statement 2; Statement 3; …. . ; ……; }
The else Clause § An if statement may have an else clause § Syntax: if(expression) statement; else statement; § Example: if(i>j) max=i; else max=j;
Nested if § if statement inside if is called nested if § Example:
Cascaded if § We often need series of test condition stopping them when one is true
Logical Operator § An operator that acts on binary value to produce a result according to the laws of Boolean logic (e. g. the AND, OR, and NOT functions). § Logical operator reduces Lines of Code in selection statement § If A holds 1 and B holds 0 then,
Logical Operator Example #1
Logical Operator Example #2
Logical Operator Example #3
Conditional Operator § Conditional operator use two symbols ? and : § Syntax: expression 1 ? Expression 2 : expression 3 § Unique C operator, needs three operands (ternary operator) § Read as if expression 1 then expression 2 else expression 3
Conditional Operator Example#1
Conditional Operator Example#2
Conditional Operator Example 3
The Switch Statement § Switch the statement according to case § Better that using cascading if
Syntax
The Switch Statement Example
- Balancing selection vs stabilizing selection
- Similarities
- K selection r selection
- Natural selection vs artificial selection
- Artificial selection vs natural selection
- Natural selection vs artificial selection
- K selection r selection
- Natural selection vs artificial selection
- Two way selection and multiway selection
- Two way selection and multiway selection in c
- Procedure of pure line selection
- Multiple selection statement in c
- Switch multiple-selection statement
- How to start a pseudocode
- Is a while loop a selection structure
- Multiple selection statement
- Chapter 6 employee testing and selection ppt
- Chapter one the selection of a research approach
- Process selection and facility layout pdf
- Chapter 6 process selection and facility layout
- Chapter 15 section 1 darwins theory of natural selection