C if else

Introduction

The if statement is used to conditionally execute a statement or a block of statements. Conditions can be true or false, execute one thing when the condition is true, something else when the condition is false.

Syntax:

if (expression)

statement(s);

[else statement(s);]

The expression after the 'if' is called the condition of the if statement. If the expression is evaluated to true, the statement(s) following 'if' clause execute and if the expression evaluates to a false value, the statement following the 'else' clause execute. See the following pictorial presentation and a simple example of if-else statement.

c if else statement

Example :

 
  1. if (a == b)   
  2.   printf ("%d is equal to %d", a, b);  
  3. else  
  4.   printf ("%d is not equal to %d", a, b);  

If-then statements:

If we want to execute some statement(s) under a given condition and nothing otherwise then the syntax will be :

if (expression)
statement(s);

Example :

 
  1. if (a == b)  
  2.     printf ("if a is equal to b");  
  3.     printf ("%d is equal to %d", a, b);  

Note : There is no indentation rule in writing C programming, we can write the above code in following ways :

 
  1. if (a == b)  
  2.    printf ("if a is equal to b");  
  3.    printf ("%d is equal to %d", a, b);  
 
  1. if (a == b)  
  2.      {  
  3.     printf ("if a is equal to b");  
  4.     printf ("%d is equal to %d", a, b);  
  5.      }  

The second way of writing code is a good practice.

A complete example on conditional if-else statement

The following program takes an integer (an integer is a number that can be written without a fractional or decimal component) as input from the user and find whether the given number is positive or negative.

 
  1. #include<stdio.h>  
  2. main()  
  3. {  
  4.  int num;  
  5.  printf("Input a number : ");   
  6.  scanf("%d",&num);  
  7.  if(num>0)  
  8.  {  
  9.   printf("This is a positive integer\n");  
  10.  }  
  11.  else // else portion of if statement  
  12.  {  
  13.   printf(" This is not a positive integer..Try again\n");  
  14.  }  
  15. }  

The following program works same as that of the above program. But here we use two separate if-statements.

 
  1. #include<stdio.h>  
  2. main()  
  3. {  
  4.  int num;  
  5.  printf("Input an integer : ");   
  6.  scanf("%d",&num);  
  7.  if(num>0)  
  8.  {  
  9.   printf("This is a positive integer\n");  
  10.  }  
  11.  if(num<=0)  
  12.  {  
  13.   printf(" This is not a positive integer..Try again\n");  
  14.  }  
  15. }  

Sequential if-then statements

You can use sequential if-statements when conditions are not mutually exclusive. See the following example where we want to print messages to tell which of three variables (between a, b, c) are the same :

 
  1. if (a == b)  
  2.   printf ("a = b");  
  3. if (a == c)  
  4.   printf ("a = c");  
  5. if (b == c)  
  6.   printf ("b = c")  

Multiway if-else-Statement

The syntax of the multi-way if-else-statement is :

 
  1. if (expression_1)  
  2.     statement_1  
  3. else if (expression_2)  
  4.     statement_2  
  5. .  
  6. .  
  7. .  
  8. else if (expression_n)  
  9.     statement_n  
  10. else  
  11.     other_statement  

At the time of execution the compiler will check the expression (condition) one after the other, if any expression (condition) is found to hold true, the corresponding statement will be executed. If none of the expression (condition) is true then the statement after the last else will be executed.

Example :

For example there is a grading system in an examination evaluation. The rules are defined in the following table.

Score Grade
>=90% Excellent
Between 80% and 89% Very Good
Between 60% and 79% Good
Between 50% and 59% Average
Between 40% and 49% Poor
<40% Not promoted

The following program determine the grade of a student according the rules of the said table. The program need to input score of a student.

 
  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. int num;  
  5. printf("Input score :");  
  6. scanf("%d",&num);  
  7. if (num>=90)  
  8.  {  
  9.   printf("Grade : Excellent");  
  10.  }  
  11. else if(num>=80 && num<90)  
  12.  {  
  13.   printf("Grade : Very Good");  
  14.  }  
  15. else if(num>=60 && num<80)  
  16.  {  
  17.   printf("Grade : Good");  
  18.  }  
  19. else if(num>=50 && num<60)  
  20.  {  
  21.   printf("Grade : Average");  
  22.  }  
  23. else if(num>=40 && num<50)  
  24.  {  
  25.   printf("Grade : Poor");  
  26.  }  
  27. else   
  28.  {  
  29.   printf("Grade : Not Promoted");  
  30.  }  
  31. }  

Here is the flowchart of the said program :

c  if else flowchart

Nested if-then-else statements

Each if-statement is itself a statement, so it can be nested inside another if statement. Nesting can go on indefinitely. See the following example (nested if-else is used), which find minimum among three numbers.

 
  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. int num1=5, num2=3, num3=-12, min;  
  5.  if(num1<num2)  
  6.   {  
  7.    if(num1<num3)  
  8.      min = num1;  
  9.    else  
  10.      min = num3;  
  11.   }  
  12.  else  
  13.   {  
  14.    if(num2<num3)  
  15.     min = num2;  
  16.     else  
  17.     min = num3;  
  18.   }  
  19.  printf("Among %d, %d, %d minimum number is %d",num1,num2,num3,min);  
  20. }  

Output

Among 5, 3, -12 minimum number is -12

 

source:http://www.w3resource.com/c-programming/c-if-else-statement.php