Hey guys, what is up. In this particular post, we will be talking about if else in c++. Also, it’s syntax and we will see some examples on if else in c++.

If else in C++

If else are also known as decision-making statements or conditional statements. That allows the program to execute according to the condition provided. In this structure, a set of statements are executed depending upon the condition to be true or false.

There are 3 types of if else statements in C++ 

  • if statement
  • if else statement
  • else if ladder

If statement 

It is used when we want to test a condition, if a given condition is true then if statement will be executed otherwise it will skip that block of statement.

SYNTAX:

If (condition)

{

     //block of statements;

}

Example of if statement:

#include<iostream.h>

#include<conio.h>

void main()

{

                 clrscr();

                 int n;

                 cout<<“ENTER N: ”;

                 cin>>n;

                 if(n==5)

                 {

                             cout<<n;

                 }

                 getch();

}

OUTPUT OF THIS EXAMPLE:

output of if code in C++

If else statement:

It is used to perform two operations for a single condition. If the given condition is true, then if block is executed otherwise else block is executed.

Syntax 

if (condition)

{

    //block of statements

}

else

{

    //block of statements

}

Example of if else:

#include<iostream.h>

#include<conio.h>

void main()

{

                 clrscr();

                 int n;

                 cout<<“ENTER N: ”;

                 cin>>n;

                 if(n==5)

                 {

                             cout<<n;

                 }

                 else

                 {

                             cout<<“WRONG NUMBER: ”;

}

getch();

}

OUTPUT OF THIS EXAMPLE:

output of if else code

Else if ladder:

It is used to perform multiple cases for different conditions. In this statement, there is one if condition and multiple else if condition and one else block. It is also called a nested if-else statement.

Syntax –

If (condition 1)

{

     //statements;

else if (condition 2)

{

    //statements;

}

else if (condition 3)

{

    //statements;

}

else 

{

    //statements;

}

Example of else if ladder:

#include<iostream.h>

#include<conio.h>

void main()

{

    clrscr();

    int n;

    cout<<”ENTER THE VALUE OF N = ”;

    cin>>n;

    if(n>0)

    {

        cout<<” THE NUMBER IS POSITIVE”;

    }

    else if(n<0)

    {

        cout<<” THE NUMBER IS NEGATIVE”;

    }

    else

    {

        cout<<” THE NUMBER IS EQUAL TO ZERO”;

    }

    getch();

}

OUTPUT OF THIS EXAMPLE:

output of else if ladder code

NOW LET US SEE FEW EXAMPLES ON IF ELSE IN C++

Ques: To find whether a number is odd or even using if else in c++?

#include<iostream.h>

#include<conio.h>

void main()

{

    clrscr();

    int n;

    cout<<”ENTER THE VALUE OF N = ”;

    cin>>n;

    if(n%2==0)

    {

        cout<<” THE NUMBER IS EVEN”;

    }

    else

    {

        cout<<” THE NUMBER IS ODD”;

    }

    getch();

}

output of odd even code

Ques: To find whether a given year is leap year or not?

#include<iostream.h>

#include<conio.h>

void main()

{

    clrscr();

    int year;

    cout<<”ENTER THE YEAR = ”;

    cin>>year;

    if(year%4==0)

    {

        cout<<” THIS IS A LEAP YEAR”;

    }

    else

    {

        cout<<” THIS IS NOT A LEAP YEAR”;

    }

    getch();

}

output of leap year code using if else in c++

In the upcoming post, you will be getting information related to one of the latest technology. If you haven’t read about what are headers files in c++ then go and read this. Also, stay tuned for the upcoming post

Categories: CODING

0 Comments

Leave a Reply

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