If-else in C

If-else in C


 

What is If - Else ?

If-else statement in C is used to perform the operations specified in if portions are executed if and only if the given condition is true and it's based on some specific condition.
 
Types of if statements in C language :
1. If statement
2. If-else statement
3. If else-If statement
4. Nested if statement
 

1. If statement :

If statements stands for modifying the flow of execution of a program. It evaluated the condition first before executing any statements inside the body of it. 

Syntax :
if(condition) 
{
 // statement will be executed if the condition is true 
}

Sample Program :
#include<stdio.h>
int main ()
{
	int x = 2;
	// checking the boolean condition  
	if( x < 10 )
	{
	// if condition is true then it'll print the following 
		printf("%d is smaller than 10\n",x );
	}
	else 
	{
	// if condition is false then it'll print the following 
		printf(" x = %d\n", x);
	}
	return 0;
}

Output :


2. If-else statement :

It is an extended version of If statement. Here we use if statement for one condition and else statement to do something if the if condition is false.
 
Syntax :
if( expression) 
{ 
    statement;  /*if the expression is true then it will print this part.*/
} ; 
else 
{ 
    statement;  //if the expression is false then it will print this part.
};

Sample Program :
#include<stdio.h>
int main () 
{
  int T;
  printf ("Enter a number : ");
  scanf ("%d", &T);
  if (T % 2 == 0)
  {
      printf ("%d is an even number.", T);
  }
  else
  {
      printf ("%d is an odd number.", T);
  }
  return 0;
}

Output :



3. If else-If statement :

Deciding among multiple options we use if else-if statement or if else-if ladder. As one of the if condition is true then the associated statements with that if condition will be executed, and the rest of the ladder will be bypassed. And if none of the conditions get true then the last else statement will be executed.

Syntax :
if (expression) 
{ 
    statement;    // if the expression is true this part will be printed.
} ;
else if (another expression)
{
    statement;    // if the another expression is satisfied print this part will be printed.
};
else
{
    statement;    // if all the expression is false then this part will be printed.
};
  
  
Sample Program :
#include<stdio.h>
int main () 
{
int a, p;
printf ("enter the first value : ");
scanf ("%d", &a);
printf ("\nenter the second value : ");
scanf ("%d", &p);
if (a > p)
    {
        printf ("\n %d is greater than %d", a, p);
    }
else if (p > a)
    {
        printf ("\n %d is greater than %d", p, a);
    }
else
    {
        printf ("\nBoth %d and %d are equal", a, p);
    }
return 0;
}

Output :



4. Nested if statement :

Nested if statement is nothing but an if statement inside another if statement.

Syntax :
if( main expression )
{
    //will be executed when condition 1 is true.
    if(nested expression )
    {
        // will be executed when condition 2 is true.
    }
}

Sample Program :
#include<stdio.h>
int main ()
{
    int mark;
    printf ("enter the marks to check : ");
    scanf ("%d", &mark);
    if (mark >= 50) 
    {
        // Nested if 
        if (mark == 100) 
        {
            // This will be executed when above statement is true.
            printf("Perfect!!");
        }
    printf(" You passed.");
    }
    else 
    {
        printf("You failed. Try harder");
    }
}

Output :