Java Programming

5.1 If statement

if statement
An if statement contains a Boolean expression and block of statements enclosed within braces.

if(conditional expression)
//statement or compound statement;
else
//optional
//statement or compound statement;
//optional

If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to next statement without executing Statement block.

if….else
If statement block with else statement is known as as if…else statement. Else portion is non-compulsory.

if ( condition_one )
{
//statements
}
else if ( condition_two )
{
//statements
}
else
{
//statements
}

If the condition is true, then compiler will execute the if block of statements, if false then else block of statements will be executed.

nested if…else
when a series of decisions are involved, we may have to use more than one if…else statement in nested form as follows:

if(test condition1)
{
if(test condition2)
{
//statement1;
}
else
{
//statement2;
}
}
else
{
//statement3;
}
//statement x;

Download for more knowledge

https://play.google.com/store/apps/details?id=ab.java.programming

Leave a comment