Upcoming SlideShare
×

# Selection Statements in C Programming

2,717 views
2,362 views

Published on

This slide provides the introduction to the all the selection statements available in C Programming.

2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
2,717
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
98
0
Likes
2
Embeds 0
No embeds

No notes for slide

### Selection Statements in C Programming

1. 1. LOGICAL EXPRESSIONS  IF STATEMENT SWITCH STATEMENT Selection Statements
2. 2. Flow of Control  Unless specified , the order of statement execution through a C program is linear: one statement after the other, in sequence.  Some programming statements modify that order, allowing us to:  decide whether or not to execute a particular statement, or perform a statement over and over, repetitively
3. 3. 3 Flow of Control  These decisions are based on a boolean Or logical expression (also called a condition) that evaluates to true or false  The order of statement execution is called the flow of control
4. 4. Flow of Control Sequential Flow
5. 5. Flow of Control Selection Statements
6. 6. Flow of Control Repetition
7. 7. Logical Expression  Logical expression is an expression which uses one or more logical operators, e.g.,  (temperature > 90.0 && humidity > 0.90)  !(n <= 0 || n >= 100).  The output of the logical expression is the boolean value either true or false.
8. 8. If Statements  If statements consists of boolean expression followed by one or more statements.  If the boolean expression evaluates to true, the statements inside the block get executed otherwise the first statement outside the block get executed.  The false value is o and all the other values are evaluated as true in C.
9. 9. If Statement  The syntax of an If statement in C Program is given below
10. 10. If Statements
11. 11. If Statement(Example)
12. 12. Output
13. 13. If…else Statement  If statements can be followed by the optional else statements, which get executed when the boolean expression is false.
14. 14. If…else Statement
15. 15. If…else Statement(Example)
16. 16. If…else Statement
17. 17. If…elseif…else Statement  If statement can be followed by optional elseif..else statement, which is very useful to test various conditions using single if…elseif statement.  Following things should be kept in mind  An if can have zero or one else's and it must come after any else if's.  An if can have zero to many else if's and they must come before the else.  Once an else if succeeds, none of the remaining else if's or else's will be tested.
18. 18. If…elseif…else Statement
19. 19. If…elseif…else Statement(Example) #include <stdio.h> #include<conio.h> int main () { int a = 100; if( a == 10 ) { printf("Value of a is 10n" ); } else if( a == 20 ) { printf("Value of a is 20n" ); } else if( a == 30 ) { printf("Value of a is 30n" ); } else { printf("None of the values is matchingn" ); } printf("Exact value of a is: %dn", a ); getch(); return 0; }
20. 20. If…elseif…else Statement
21. 21. Nested if Statements  It is always legal in C programming to nest if-else statements, which means we can use one if or else if statement inside another if or else if statement(s).
22. 22. Nested if Statements #include <stdio.h> #include <conio.h> int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { printf("Value of a is 100 and b is 200n" ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); getch(); return 0; }
23. 23. Nested if Statements
24. 24. Switch Statement  A switch statement allows a variable to be tested for equality against a list of values.  Each value is called a case, and the variable being switched on is checked for each switch case.  The following rules apply to a switch statement:  The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.  The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
25. 25. Switch Statement  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
26. 26. Switch Statement
27. 27. Switch Statement
28. 28. Switch Statement #include <stdio.h> #include <conio.h> int main () { char grade = 'B'; switch(grade) { case 'A' : printf("Excellent!n" ); break; case 'B' : case 'C' : printf("Well donen" ); break; case 'D' : printf("You passedn" ); break; case 'F' : printf("Better try againn" ); break; default : printf("Invalid graden" ); } printf("Your grade is %cn", grade ); getch(); return 0; }
29. 29. Switch Statement
30. 30. Nested Switch Statements  It is possible to have a switch as part of the statement sequence of an outer switch.  Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.
31. 31. Nested Switch Statements
32. 32. Nested Switch Statements #include <stdio.h> #include <conio.h> int main () { int a = 100; int b = 200; switch(a) { case 100: printf("This is part of outer switchn", a ); switch(b) { case 200: printf("This is part of inner switchn", a ); } } printf("Exact value of a is : %dn", a ); printf("Exact value of b is : %dn", b ); getch(); return 0; }
33. 33. Nested Switch Statements