So far, we have seen that all the statements in a C program gets executed sequentially in the order in which they are written and appear.
Decision power enable the deciding order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.
We will learn to change the order of execution of statements in this chapter and repetition in next program.
# C Branching Add decision power to computer
This enable us to specify conditions inside the program ,so computer will perform certain function only if the condition is meet.
We generally have 4 types of Branching statements :
- if statement
- switch statement
- conditional operator statement (? : operator)
- goto statement
# Single Conditional Branching
# if statement
Here is a simple C program demonstrating an if statement:
This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the
Boolean expression evaluates to True, then C executes the single line immediately following the if statement (or a block of lines within braces immediately following the if statement). If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.