Tuesday, March 24, 2009

The if..else Decision Construct

The if..else Construct

It is a decision construct
The if decision construct is followed by a logical expression in which data is compared and a decision is made based on result of comparison.That is take it in a lighter way suppose you are going to watch a movie and in case you found house full board hanging out then you must have a choice that is if there is a houseful the i am gonna watch blah blah movie.

Follow like this :-

if( ! houseful ){
watch movie a
}
else {
watch movie b
}

Similarly the Syntax is :-

If(boolean_expr)
{
statements;
}
else
{
statements;
}

Example:-

To find equality of two numbers

if(num1!=num2)
{
System.out.println(num1+"not equal to"+num1);
}
else
{
System.out.println(num1+"is equal to"+num2);
}

This example finds equality of two numbers.If the value of first number is not equal to that of second then if block executes and prints message when if block completes execution
the control never goes to else block and resumes simple program flow.Same is the case is with else block it will be executed when the boolean expression in if is false and after completion program continues its normal flow.Always if comes before else keep in mind.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP