07:57 -
OOPS using C++
Explain all the if … else statements in C++ with a small code for each.
There are tow types of if….else statements in C++
which are explained below.
Simple
if….else statement
Syntax: if(expression or condition)
{ statement
1;
statement 2;
}
else
{ statement
3;
statement 4;
}
The expression or condition is any expression built
using relation operators which either yields true or false condition. If no
relational operator are used for comparison, then the expression will be
evaluated and zero is taken as false and non zero value is taken as true.
If the condition is true, statement1 and statement2
is executed otherwise statement3 and statement4 is executed. Else part is
optional. If there is no else part, then the next statement after the if
statement is executed, if the condition is false.
Example:
Find even odd
numbers.
//evenodd.cpp
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<”Please enter a number”<< endl;
cin>>num;
if((num%2)==0)
{
count<<num<<”is a even
number”;
}
else
{
cout<<num<<”is a odd
number”;
}
getch();
}
Nested If statement
If
statement can be nested in another if statement to check multiple condition.
Syntax:
if(condtion1)
{
if(condtion2)
{ statement1;
statement2;
}
else
if(condtion3)
{ statement3;
}
}
Example: Fine
large number.
//Large.cpp
#include<iostream.h>
void
main()
{
int a,b,c;
cout<<”Please
enter three numbers.”;
cin>>a>>b>>c;
if((a>b)
&& (b>c))
count<<a<<”is the largest
number”;
else
if((b>a) && (b>c))
count<<b<<”is the largest
number”;
else
if((c>a) && (c>b))
count<<c<<”is the largest
number”;
}