Tuesday, 20 December 2011

Episode 10 : if else statement


if else statement

In programming C, we can use if else statement like only if statement but some little difference between them. Suppose we write a program which is count boys and girls in a class. Here ‘M’ for boys and ‘F’ for girls . Now we write this program only using if statement.

If(count == ‘M’)
++boy;
If(count == ‘F’)
++girls;

Here first computer looking that when value of count is ‘M’ then increase boy is one and when value of count is ‘F’ then increase girl is one. In this way, there only increase run time else not and it is just waste of time.

Now we write another code where we identify boy or girl in one if condition and else statement. Look at that,

If(count == ‘M’)
++boy;
else
++girl;
Here first computer looking that when value of count is ‘M’ then increase boy is one neither girl is one.

Finally, The main structure of this if-else statement is :

          if (condition)
          statement 1;
          ----------------
          Statement n;
          else
          statement 2;
          ----------------
          Statement n;

I hope you are all understand this matter.

Written by “Shojib”

No comments:

Post a Comment