Jun 18, 2014

C Programming #18: Decision Making - if, else if, else

Decision making construct are very important in programming. They help in choose different paths of program execution based on some condition.

Following article will discuss the if - else type of Decision making


Simple if

Syntax of simple if is

if (condition) {
   // Do something if the condition is true.
}

condition is a expression that returns true or false.

Following program shows the use of Simple if

#include <stdio.h>
int main()
{
   int a = 20;
   
   if(a > 10) {
      printf("Value of a is greater than 10\n");
   }
   if(a < 10) {
      printf("Value of a is less than 10\n");
   }
   return 0;
}

Output of the above program is


Value of a is greater than 10

Note the if there is only one statement after if then curly brases can be omitted.
But is highly encouraged to used it always.

if, else

Syntax of if, else

if (condition) {
   // Do something if the condition is true.
} else {
   // Do something if the condition is false.
}

Following program shows the use of Simple if


#include <stdio.h>
int main()
{
   int a = 5;
   
   if(a > 10) {
      printf("Value of a is greater than 10\n");
   }else {
      printf("Value of a is less than or equal to 10\n");
   }
   return 0;
}

Output of the above program is


Value of a is less than or equal to 10

First a > 10 is evaluted, in our case it is false; hence the statements inside else is executed.

if, else if, else

Syntax of if, else if, else

if (condition1) {
   // Do something if the condition1 is true.
} else if (condition2) {
   // Do something if the condition1 is false and condition2 is true
} else {
   // Do something if both conidition is false
}

Note
  1. there can any number of else if statement
  2. else is optional.
Following program shows the use of if, else if, else


#include <stdio.h>
int main()
{
   int a = 12;
   
   if((a > 0) && (a <= 5)) {
      printf("Value of a is between 0 to 5\n");
   }else if((a > 5) && (a <= 10)) {
      printf("Value of a is between 5 to 10\n");
   }else if((a > 10) && (a <= 15)) {
      printf("Value of a is between 10 to 15\n");
   }else {
      printf("Value of a is greater than 15");
   }
   return 0;
}

Output of the above program is


Value of a is between 10 to 15

First (a > 0) && (a <= 5) is evaluated
(a > 0) && (a <= 5) => (12 > 0) && (12 <= 5) => 1 && 0 => 0 => false
Then (a > 5) && (a <= 10) is evaluated
(a > 5) && (a <= 10) => (12 > 5) && (12 <= 10) => 1 && 0 => 0 => false
Then (a > 10) && (a <= 15) is evaluated
(a > 10) && (a <= 15) => (12 > 10) && (12 <= 15) => 1 && 1 => 1 => true
Then printf("Value of a is between 10 to 15\n"); is evaluated.
Later condition are not evaluated at all. Above sentences are called as dry running the code against a particular value. Use this technique often then help to bug out most of the mistakes.

Links

Quiz - Not Yet written !!
Next Article - C Programming #19: Decision Making - Switch
Previous Article - C Programming #17: Comments
All Article - C Programming

No comments :

Post a Comment