if-else statement in c
if else statement in c programming

If you want to know about if else statement in c programming language then read this blog post completely.

What is if else statement in c programming

If else is a control statement. It also decides which block to execute on the basis of condition.

Here any one block of code is executed. If the condition becomes true then the true block is executed otherwise the false block is executed.

If else statement is extended form of simple if statement.

General form

if (condition met)
   {
     true block ;
   }
else 
   {
     false block;
   }

let us take one example

if (grade >75)
   {
     printf("you secured distinction \n");
     printf("congrates \n");
   }
else
   printf(" you have not secured distinction \n");

In the above example if the grade is more than 75 then we execute a true block statement i.e. we print statements such as “you secured distinction”, “congrats”. otherwise, we execute a false block statement i.e. statement followed by else keyword.

If else in c examples

Example program 1

//if else statement in c example 1
#include <stdio.h>
#include<stdlib.h>
int main()
{
  int b=1000,age;
   printf("\n\n\tEnter your age \n\t");
   scanf("%d",&age);
  if(age >= 50)
   {
      printf("\n\tyou are a senior citizen \n");
      printf("\tyour promotion bonus is %d \n\n",b);
   }
   else
   {
      printf("\n\tyou are not a senior citizen \n");
      printf("\tyou have no promotion bonus \n\n");
   }

  return 0;
}
if else program example 1

Explanation

In above program we are asking to input your age.

Then we are checking the age for promotion bonus if age is equal to or greater than 60 using >=.

If age is equal to or greater than 60 then we are printing “you are a senior citizen” and “your promotion bonus is 1000” in a new line

If age is lesser than 60 then else block will be executed hence we print “you are not a senior citizen” and “you have no promotion bonus” in a new line.

Example program 2

//if else statement in c example 2
#include <stdio.h>
#include<stdlib.h>
int main()
{
  int b,number;
   printf("\n\n\tEnter a number \n\t");
   scanf("%d",&number);
  if( (number % 2) == 0 )
   {
      printf("\n\t%d is an even number\n\n", number );
   }
  else
  {
  	  printf("\n\t%d is an odd number\n\n", number );
  }
  return 0;
}
if else statement program example 1

Explanation

Here we are asking user to input a number.

After successful reading of that number from the user, we check for odd or even number finding its remainder after dividing by 2.

If the remainder is equal to zero then it is even number. Hence we print that the number is even number.

Else the remainder is not equal to zero that the remainder has a value of 1 then we can say that it is an odd number. Hence we print that the number is an odd number.

If you want to read more on Odd even in C | C++ and Java read this blog post.


Difference between if and if else statement

If statement executes only true block statement. Else if executes either true block or false block statement.

All the statements in if block are executed if the condition is met otherwise no statements will be executed. In else if any one block is executed.

In if statement if is a keyword. In else if “else” and “if” are keywords.

If is a simple form of conditional statement. else if is a extended form of if statement.

General forms are as follows

if(condition is true)
    execute the statement;

if (condition met)
   {
     true block ;
   }
else 
   {
     false block;
   }

if statement in celse if statement in c
Executes only true block statement.Executes either true block or false block statement.
if(condition is true)
execute the statement;
if (condition met)
{
true block ;
}
else
{
false block;
}
All the statements in if block is executed if condition is met otherwise no statements will be executed.Any one block is executed.
If is a keyword.If, else are keywords.
Simplest form.Extended form of if statement.

Difference between if else and nested if else statement

Nested else if statement checks multiple conditions. else if executes either true block or false block statement.

In nested if else statement nesting can be done at any level. In else if statement nesting can not be done.

If there are many conditions nested if else becomes complex statement. In else if there is no complexity.

General forms are as follows

if (condition met)
  {
   if (condition met)
    {
      true block;
    }
   else
    {
      false block;
    }
  }
else
   {
     false block;
   }


if (condition met)
   {
     true block ;
   }
else 
   {
     false block;
   }
nested if else in cif else in c
Checks multiple conditions.Executes either true block or false block statement.
Nesting can be done at any level.Nesting can not be done.
if (condition met)
{
if (condition met)
{
true block;
}
else
{
false block;
}
}
else
{
false block;
}
if (condition met)
{
true block ;
}
else
{
false block;
}

Difference between if-else and switch statement

In switch the matched case block is executed. if else executes either true block or false block statement.

In switch any of the matched case block is executed otherwise default statement is executed. If else either true block or false block is executed.

In switch multiple conditions are checked. In if lese only one condition is checked.

In switch statement switch,case,default are keywords. In if else statement if and else are keywords.

General forms are as follows

switch( integer expression)
  {
     case constant 1: block1;
     case constant 2: block2;
      .
      .
     default:
              default block;
  }               


if (condition met)
   {
     true block ;
   }
else 
   {
     false block;
   }
else if in cswitch in c
Executes either true block or false block statement.The matched case block is executed.
Either true block or false block is executed.Any of the matched case block is executed otherwise default statement is executed.
Only one condition is checked.Multiple conditions are checked.
if (condition met)
{
true block ;
}
else
{
false block;
}
switch( integer expression)
{
case constant 1: block1;
case constant 2: block2;
.
.
default:
default block;
}

People also ask for

Why we use if-else in c?

It is used to control the program flow. Depending upon the condition met the block of code will be executed.

What happens if you put a semicolon after an if statement?

If we put a semicolon after the if statement the following block of code does not execute. It terminates the process.

Can we compare strings using if statement?

No, we cannot compare strings using the if statement. The condition should be a Boolean variable or arithmetic expression.

What is null statement?

The null statement represents nothing in c. It is an empty statement.

What is if else in c programming?

If else is a control statement. It also decides which block to execute on the basis of condition. If the condition is satisfied then it executes code in if block. Otherwise, it executes code in the else block.


Also read