If statement in C - if,else,else if and nested if else statement | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Monday, June 24, 2019

If statement in C - if,else,else if and nested if else statement | My CS Tutorial

In this tutorial we will learn what is if,if else,else if and nested if else statement in C language.

C if statement:

We use if statement in C language for execute a statement only when given condition of if statement is true otherwise the statement is not execute. If the condition is not true the statement is skipped.

Syntex of if statement:

if(condition)
{
    // GIVEN STATEMENT
}

Flow diagram of if statement






Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n enter the value of a=");
scanf("%d",&a);
printf("\n enter the value of b=");
scanf("%d",&b);
if(a>b)
{
printf("\n a is greatest");
}
getch();
}


Output 1:






Explanation:

In this C program if statement test the condition (a>b).If the condition is true the statement or body of if statement will be execute otherwise the statement will be skip.


C If else statement:


In C if statement,We use if statement in C language for execute a statement only when given condition of if statement is true otherwise the statement is not execute. If the condition is not true the statement is skipped.But in if else statement if the condition is not true the statement will be executed also.Else statement have own statement.If the condition is false the else statement will be executed.

Syntax of if else statement:


if(condition)
{
    // GIVEN STATEMENT OF IF STATEMENT
}
else
{
   // GIVEN STATEMENT OF ELSE STATEMENT
}


Example:


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n enter the value of a=");
scanf("%d",&a);
printf("\n enter the value of b=");
scanf("%d",&b);
if(a>b)
{
printf("\n a is greatest");
}
else
{
printf("\n b is greatest");
}
getch();
}


Output 1:




Output 2:







Explanation:

In the if statement it is test the condition (a>b).If the condition is true the statement or body of if statement will be execute otherwise the statement will be skip.But in the if else statement,if the condition is true the if statement will be executed if condition is false the else statement will be executed.


C else if statement:



In C language, else if statement like a if statement.It is use when if statement have many conditions.


Syntax of else if statement:


if(condition)
{
    // GIVEN STATEMENT OF IF STATEMENT
}
else if
{
    // GIVEN STATEMENT OF ELSE IF STATEMENT
}
else
{
   // GIVEN STATEMENT OF ELSE STATEMENT
}


Example:




#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("\n enter the number between 1 to 3=");
scanf("%d",&num);
if(num==1)
printf("one");
else if(num==2)
printf("two");
else if(num==3)
printf("three");
else
printf("wrong entery");
getch();
}


Output 1:



Output 2:




Explanation:

In this program we will take a variable (num) in int. And entered the value of num.In this program we use many conditions so we will use else if statement. In the first,if statement will check our condition if condition is true statement will be executed otherwise check next conditions. Which condition is true that's statement will be executed.

C nested if else statement:


In C programming nested if else statement is a important feature. When we use if or else statement inside a if statement or else statement this is called nesting of if else statement.


Syntax of nested if else statement:




Example:


#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=25;
clrscr();
    if(a==10)
    {
        if(b==25)
        {
            printf("value of a is 10, and value of b is 25");
        }
    }
    getch();
}



Output:






We use if statement in C language for execute a statement only when given condition of if statement is true otherwise the statement is not execute. If the condition is not true the statement is skipped. Where in else statement,If the condition is not true the statement is skipped.But in if else statement if the condition is not true the statement will be executed also.Else statement have own statement.If the condition is false the else statement will be executed.
 

In C language, else if statement like a if statement.It is use when if statement have many conditions.
                        When we use if or else statement inside a if statement or else statement this is called nesting of if else statement.


I hope that you understand everything easily.If you have any problem or question so you write a comment and follow me on this blog and my Facebook page and on Instagram.












No comments:

Post a Comment