Tuesday, May 12, 2009

If-else Statements

The if-else statements
It is used to control the flow of execution and also used carry out the logical test and ten pickup one of the two possible actions depending on the logical test.
It is used to execute some statements when the condition is true and execute some other statements. When the conditions is false.

Syntax:
if (condition)
{
statements 1;
}
else
{
statements 2;
}
statements 3;

If the conditions is true, then the statements 1 and statements 3 will be executed. The true statements may be a single statements or group of statements. If the condition is false then the statements 1 are not executed but statements 2 and statements 3 will be executed.

Example:
if (a>=10)
{
cout<<”A is greater than or equal to 10”;
}
else
{
cout<<”A value is less than 10”;

}
cout<<”A value is :” << a;

The above example : If a =15 the condition is true output will be proved “A is greater than or equal to 10” .and A Value is : 15

If a = 6the condition is false output will be proved ”A value is less than 10”. A value is : 6

Example:

Header files
Class biggest
{
Int a,b;
Public:
Void get();
Void put();

};
void biggest::get()
{
cout<<”Enter the Value of A and B:”;
cin>> a >> b;
}
void biggest::put()
{
if(a>b)
{
cout<<”A is biggest number”;
}
else
{
cout<<”B is biggest Number”;
}
cout<<”A = “ <getch();
}

Output will be
Enter the Value of A and B:
15
24

B is biggest Number
A=15 B= 24

No comments:

Post a Comment