If else statement – free C programming tutorial

In this tutorial, you will be familiarize with one type of conditional statement that is if else statement available for C programming language, along with its syntax and example of if-else .

Conditional statements :

Conditional statements are those code which allows you to control the program’s flow of the execution centred on a condition.
It means that the statements in the program are not executed in sequence.
Rather, one group of statements is executed, depending on how the condition is evaluated.


if else statement:

Syntax :

if (condition) 
  {
    statement; 
  } 
 
else 
  {
      statement;
  }
statements;

How if else statement works :

If condition written in brackets() is true, then single statement or multiple statements inside the curly braces executes, otherwise it is skipped and the statements written below else will executes.
Numerical values other than zero is considered true while zero is false.

if else statement


Example of if-else statement  :

  1. Code to check whether the number entered by user is an even number or an odd number:

// Header file which contains functions for standard input and output manipulative operation.

#include <stdio.h>   

int main()  // Compiling of code start from main function
{
    int x ;  // Declaration of local variable

// To display on console screen
   printf(“Enter a value…… /n ”);

// To get input from user
   scanf(“%d”,&x);

// Use of if-else statement
// to check whether the number entered by user is an even or an odd number.
     if ( x % 2 == 0)
          {
               printf(“Entered number is an even number.”);
          }
     else
         {
           printf(“Entered number is an odd number.”);
          }
}

Here I include both the possible aspects for this code such as entered number is an even number like 100 or it may be an odd number like 145.
Output of both the cases shown below :

case 1.  if entered value is 100 then
OUTPUT:

Enter a value……100
Entered number is an even number.

But,
case 2. if entered value is 145 then
OUTPUT:

Leave a Comment

Your email address will not be published. Required fields are marked *