Branching and looping in C
In Real life we come across many situations in which we have multiple options to choose. To implement same kind of situation in C programming we have if and else keywords.
There might be scenario in real life where we have to do same thing again and again till we get required outcomes. To have support of this scenario C Language provides us way of implementing looping.
If and Else
suppose you have 10000 bucks in your pocket and you want to purchase electronics item like fridge or television but your preference is to buy Television. Before going to market you are sure that fridge price will be less than 10000 but you are not sure of Television’s price. So while going to market you decide in your mind if price of TV will be less than 10000 you will purchase TV otherwise you will purchase Fridge. Same scenario we can understand in a simple format by using If and else keyword.
If TV price is less than 10000
purchase TV
else
purchase fridge
to implement above situations in C language we have privilege to use If and Else keywords. Below is the basic use case of IF and else
if(expression)
statement;
else
statement;
expression here refer to some condition to choose between if or else. If expression results comes to true or some non-zero value then if part statement will be executed otherwise else part statement will be executed.
example:-
int i=10;
if(i>11)
printf(“if part”);
else
printf(“else part”);
in the above example we i is initialized to 10. while executing if expression we check weather value of i is greater than 11 then print “if part” else print “else part”.
In simple words we can say if expression result will come true then “If part” will execute otherwise “else part ” will be executed.
below are some scenario of If expressions:
if(0) => false
if(1) =>true
if(25) => true
if(-1) =>true
if(-20) => true
Till now we have discussed if and else keywords in which we have only two options either “if part” or “else part” . But there might be where we have more than two option and we have to choose one options among all available options.
To implement this kind of scenario C provides else if keywords.
In else if case we start execution by if expression. if this expression comes true we execute the if part otherwise we go ahead and execute each else if expression . If we find any else if expression to be true then that else if part executed. otherwise else part will be executed .
example:-
i=20;
if(i=10)
{
printf(“if part”);
}
else if (i=13)
{
printf(“else if part 1”);
}
else if (i=20)
{
printf(“else if part 2?);
}
else
{
printf(” else part “);
}
in above example as value of i equals 20. we first execute if expression in which we are comparing value of i with 10. As i is not equal to 10 we will go ahead and will execute else if expression in which we are comparing value of i with 13. This expression result is also false because i is not equal to 13 so we go ahead and will execute another if else statement in which we are comparing i with 20 which will return a true result as i equals 20 and hence “else if part 2?will get printed.
self exercise:
1) i=10;
if(i=10)
{
printf(“if part”);
}
else if (i=13)
{
printf(“else if part 1”);
}
else if (i=20)
{
printf(“else if part 2?);
}
else
{
printf(” else part “);
}
output:- if part
2)
i=21;
if(i=10)
{
printf(“if part”);
}
else if (i=13)
{
printf(“else if part 1”);
}
else if (i=20)
{
printf(“else if part 2?);
}
else
{
printf(” else part “);
}
output:- else part