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:
Post a Comment
Post a Comment