Download presentation
Presentation is loading. Please wait.
1
Chapter 5: Selection Statement
2
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.
3
Logical Expression if statement must test the value of an expression
Relation operator is used along with if A=10; B=20;
4
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”);
5
The if statement contd…
For compound statement used braces Syntax: if (expression) { statement1; Statement2; Statement3; ….; …..; ……; }
6
The else Clause An if statement may have an else clause
Syntax: if(expression) statement; else Example: if(i>j) max=i; max=j;
7
Nested if if statement inside if is called nested if Example:
8
Cascaded if We often need series of test condition stopping them when one is true
9
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,
10
Logical Operator Example #1
11
Logical Operator Example #2
12
Logical Operator Example #3
13
Conditional Operator Conditional operator use two symbols ? and :
Syntax: expression1 ? Expression2 : expression3 Unique C operator, needs three operands (ternary operator) Read as if expression1 then expression2 else expression3
14
Conditional Operator Example#1
15
Conditional Operator Example#2
16
Conditional Operator Example3
17
The Switch Statement Switch the statement according to case
Better that using cascading if
18
Syntax
19
The Switch Statement Example
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.