Control Statement

if else Statement

The if-else statement in C is used to perform operations based on some specific conditions. If the given condition is true, then the operations specified in the if block are executed.

There are Following types of statements in C Language :

  • if Statement
  • if-else Statement
  • if else-if ladder
  • nested if

if Statement

if statement is used to check a given condition and perform some operations based on the correctness of that condition.

Syntax:

if (expression)
{
statement // your code
}

Example:

#include< stdio.h >
void main()
{
int a ;
printf("Enter a number : ");
printf("Enter a number : ");
scanf("%d",&a);
if(a%2==0)
{
printf("%d is even number",a ");
}
}

Output:

Enter a number : 4
4 is even number

if- else Statement

The if-else statement is used to erform two operations for a single condition. The if-else statement is an extension to the if statement,using which we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition.

Syntax:

if (expression)
{
statement // true
}
else
{
statement // false
}

Example:

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

Output:

Enter a number : 5
5 is odd number

if else-if ladder Statement

The if else-if ladder statement is an extension of the if-else statement. it is used inthe scenario where multiple cases are to be performed for diffrent coditions. In an if-else-if ladder statement, if any condition is true, then then statements defined in the if block will be executed, otherwise if any other condition is true, then the statements defined in the else-if blok will be executed. finally if none of the condition is true, then the statement defined inthe else block will be executed.

Syntax

if (condition1)
{
//code to be executed if condition1 is true
}
else if (condition2)
{
//code to be executed if condition2 is true
}
else if (condition3)
{
//code to be executed if condition3 is true
}
else
{
//code to be executed if all the condition are false
}

Example

#include< stdio.h >
{
int a ;
printf("Enter a number : ");
scanf("%d",&a);
if(a==10)
{
printf("%d is equal to 10",a ");
}
else if (a==50)
{
printf("%d is equal to 50",a ");
}
else if (a==100)
{
printf("%d is equal to 100",a ");
}
else
{
printf("%d is not equal to 10 50 100",a ");
}
}

Output:

Enter a number : 5
5 is not equal to 10 50 100

Nested if

nested if else means if-else inside if and also inside else if-else.

Syntax

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

Example

#include< stdio.h >
`
void main()
{
int a ;
printf("Enter a number : ");
scanf("%d",&a);
if (a==10)
{
if(a==20)
{
printf("i am in second if block");
}
else
{
printf("i am in second if-else block");
}
}
else
{
printf("i am in first if-else block");
}

Output:

Enter a number : 10
i am in second if-else block