In any programming language, there is a need to perform different tasks based on the condition. For example, consider an online website, when you enter wrong id or password it displays error page and when you enter correct credentials then it displays welcome page. So there must be a logic in place that checks the condition (id and password) and if the condition returns true it performs a task (displaying welcome page) else it performs a different task(displaying error page).
Using decision control statements we can control the flow of program in such a way so that it executes certain statements based on the outcome of a condition (i.e. true or false). In C Programming language we have following decision control statements.
1. if statement
2. if-else & else-if statement
3. switch-case statements
Decision Control Statements in C
Here are the tutorial links:
- If statement: The statements inside if body executes only when the condition defined by if statement is true. If the condition is false then compiler skips the statement enclosed in if’s body. We can have any number of if statements in a C program.
- If-else statement: In this decision control statement, we have two block of statements. If condition results true then if block gets executed else statements inside else block executes. else cannot exist without if statement. In this tutorial, I have covered else-if statements as well.
- Switch-case statement: This is very useful when we need to evaluate multiple conditions. The switch block defines an expression (or condition) and case has a block of statements, based on the result of expression, corresponding case block gets executed. A switch can have any number of cases, however there should be only one default handler.