In any programming language, different tasks must be performed based on the condition. Consider an online website; if you enter the wrong id or password, it displays an error page; if you enter the correct credentials, it displays a welcome page. So there must be logic in place that checks the condition (id and password) and performs a task (displaying welcome page) if the condition returns true, otherwise it performs a different task (displaying error page).
Using decision control statements, we can direct the flow of the program so that it executes specific statements based on the outcome of a condition (i.e. true or false). The following decision control statements are available in the C programming language.
1. if condition
2. if-else and else-if clauses
3. switch-case expressions
C Decision Control Statements
Here are the links to the tutorials:
If statement: The statements within the if body are only executed when the condition defined by the if statement is true. If the condition is false, the compiler skips the statement enclosed in the body of the if statement. In a C program, we can have an unlimited number of if statements.
If-else statement: There are two blocks of statements in this decision control statement. If the condition is true, the if block is executed; otherwise, the statements inside the else block are executed. Otherwise, there is no such thing as else. I also covered else-if statements in this tutorial.
When we need to evaluate multiple conditions, we can use a switch-case statement. The switch block defines an expression (or condition), and case has a set of statements that are executed based on the result of the expression. A switch can have an unlimited number of cases, but there should only be one default handler.