Decision Making Statement allows computer to decide block of code to be execute based on the condition.if, if … else,if … else if and switch statements are used as decision making statements in c programming language.
Block of statement executed only when a specified condition is true.
Syntax:
1 2 |
if( expression ) statement; |
Example:
1 2 |
if(a>b) printf("a is greater than b"); |
Above example is valid for single statement in the body of the if statement. If there is a set of multiple statements in the body of the if statement, than it is written inside a pair of parenthesis.
1 2 3 4 5 |
if(a>b) { printf("body of if statement"); printf("a greater than b"); } |
When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.