Conditional branching statement in C language
The conditional branch or branching statement is an instruction in a computer program that begin execution instruction in different order based on condition. It deviate from its default behavior which is top to bottom. The common conditional branching statements provided by C language includes if-else, switch, ternary (?:).

if-else

A if and else statement is branching statement which allows to execute single statement or block of statement based on condition. It allows to execute lines of code based on condition using if statement or either blocks of code based on condition using if and else statement.
if-else example
 1 #include <stdio.h>
 2 
 3 int main() {
 4     int a = 20, b = 10;
 5     
 6     if(b > a) {
 7         printf("Value of a is greater than b");
 8     } else {
 9         printf("Value of b is greater than a");
 10     }
 11     return 0;
 12 }

Switch

The switch statement is branching statement which allows to execute one statement or block based on the case value or condition. It also allows to specify default case if none of the case gets executed. Each block of switch statement must contain break statement otherwise it will execute all next cases once it matches a case.
switch example
 1 #include <stdio.h>
 2 
 3 int main() {
 4     char ch;
 5     
 6     printf("Enter character to perform operation : ");
 7     scanf("%c", &ch);
 8     
 9     switch(ch) {
 10         case 'A':
 11         case 'E':
 12         case 'I':
 13         case 'O':
 14         case 'U':
 15             printf("It is vowels !");
 16             break;
 17         default:
 18             printf("It is consonants !");
 19     }
 20 }

ternary operator

The ternary operator is a conditional operator that allows to write conditional and default option in single statement. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right.
ternary example
 1 #include <stdio.h>
 2 
 3 int main() {
 4     int c, a = 5, b = 10;
 5     // validating condition and assign value using ternary operator
 6     c = b > a ? b : a;
 7     
 8     printf("The value of variable c is : %d", c);
 9     return 0;
 10 }

Unconditional branching statement in C language

The unconditional branch or branching statement is an instruction in a computer program that begin execution instruction in different order without validating any condition.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us