Header Ads Widget

Responsive Advertisement

If-else Statement with Syntax and Examples in C++

There are following types of conditional statements in C++ :
  • if statement
  • if - else statement
  • nested if-else statement
  • if -else if ladder
  • switch statement

2. If-else statement - The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “else” is executed.

Syntax :

if (condition)
   // Body of the loop.
   // These statements will only execute if the condition is true.
}
else
   // Body of the loop.
   // These statements will only execute if the "if" condition is false.
}


Flow Diagram of if-else statement

       Chapter 4: The if/else (if) statement and Nested statements

Example of if-else statement in C++


#include<iostream.h>

#include<conio.h>

int main()
{
   int  a=15,b=10;
   if(a<=b)  //if condition statement
    {
       cout<<"Value of a is less than or equal to b."; //display output
    }
   else  //else condition statement
    {
       cout<<"Value of b is less than a."; //display output
    }
  getch();
}


OUTPUT:

Value of b is less than a.

Post a Comment

0 Comments