If else statement in C Language with Example

If else statement in C language is used when a different sequence of instructions is to be executed depending on the logical value (True / False) of the condition evaluated.

It’s form used in conjunction with if and the syntax for If else statement is

if (condition)
Statement _1;
else
Statement_ 2;
statement_3;

Or

if (condition)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
Statements _3_Block;

If the condition is true, then the sequence of statements (Statements_1_Block) executes; otherwise the Statements_2_Block following the else part of if-else statement will get executed.

In both the cases, the control is then transferred to Statements_3 to follow the sequential execution of the program.

if else statement
if else statement

C program to illustrate if else statement

Write a C program to print whether the given number is even or odd

/* Program to print whether the given number is even or odd*/

#include <stdio.h>

main ( )

{

int x;

printf(“Enter a number:\n”);

scanf(“%d”,&x);

if (x % 2 == 0)

printf(“\nGiven number is even\n”);

else

printf(“\nGiven number is odd\n”);

}

OUTPUT

Enter a number:
6
Given number is even

Enter a number
3
Given number is odd

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: