What are Decision Control Statements in C ?
We can make decisions in coding using decision control statement. For example, if we have two or more statements or things then we will give particular condition. Output will be executed when condition is true otherwise execution will be stop.
There are three decision control statement:
if statement : In if statement, we will a particular condition. If that condition is true then block of code inside if statement will be executed. When the condition is false then the first set of code after the end of the if statement will be executed.
Syntax: if (test expression)
{ //code }
For example: int main() {
int a = 10;
if (a< 20) /* if condition is true then print the following statement*/ {
printf(” a is less than 20″); }
printf(“value of a is : %d\n”, a);
}
Output: a is less than 20; value of a is : 10
if-else statement: In if else statements, we can give multiple statements. In which the statement is true will be executed and statements which are false are ignored.
For example:
int main() {
int n=5;
if (n%2==0) {
printf(“%d is an even”,n); // when condition is true
}
else{
printf(“%d is an odd”,n); // when condition is false
} return 0;
}
Output: 5 is an odd
Switch Case statement: In this, there are multiple statements are given from which true condition statement is executed and other statements are ignored. If none of them is true then default statement will execute.
For Example: int main() { int n=2;
switch(n + 2)
{ case 1:
printf(” case 1 : value is %d”,n);
case 2:
printf(“case 1 : value is %d”,n);
default:
printf(“Default : value is : %d”, n); }}
output: Default: value is : 2