If-else Statement in C++

if-else statement
if statement is most commonly used with the following format:

if(expression)
{
statement 1
}
else
{
statement 2
}

where else part is optional.

In this case, either of the two statements are executed depending upon the value of the expression. If the expression has a non-zero value (i.e., if the expression is true), then statement1 will be executed. Otherwise (i.e., if expression is false), statement2 will be executed.

Example:
Given here is a program which reads a number and then prints whether the given number is even or odd.

Note: If we divide a number by 2 and if the remainder is 0 then the number is EVEN else the number is ODD

#include<iostream.h>
void main()
{
int n;
cout << "Enter a number :";
cin >> n;

if(n%2 == 0)
cout << "The number is EVEN";
else
cout << "The number is ODD";
}

You may also like...

2 Responses

  1. ANANOYMUS says:

    NICE

  1. September 16, 2013

    […] If-Else Statement in C++ […]

Leave a Reply

Your email address will not be published.

%d bloggers like this: