The if statement is used to test a condition and make desicion depending upon the test condition. The test condition is evaluated and a boolean value is returned: true or false. Depending upon wheteher the condition is true or false a particulae set of statement is executed.
There are many type of if-else statement present in Java:
i)if statement
ii)if-else statement
iii)Nested if-else statement
iv)if-else-if ladder
There are many type of if-else statement present in Java:
i)if statement
ii)if-else statement
iii)Nested if-else statement
iv)if-else-if ladder
if statement
Synatx:
if(test condition)
{
//set of statements which is to be executed if the test condition is true
}
If the condition is false then the program continues with the execution of rest of the program without executing the statement(s) written inside if block.
if-else statement
The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block will be executed.
The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block will be executed.
Syntax:
if(test condition)
{
//set of statements which is to be executed if the test condition is true
}
else
{
//set of statements which is to be executed if the test condition is false
}
Please note that only one of the above blocks will get executed
Nested if-else statement
Syntax:
if(test condition 1)
{
if(test condition 2)
{
//statement 1
}
else
{
//statement 2
}
}
else
{
//statement 3
}
//statement x
if-else-if ladder
The if-else-if ladder is used to execute one statement from multiple conditions
Syntax:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgcCmsVcDpJn349Nu0xyRhQCs4s8qTHN2IUKyCzi4fE5N_BJZH6LmOZT9y-nIpLGcy56yC_UXs5dTHSzE44HslycfOo2fvvNPrqacMWa_RdjvB9MQt6D4eRnevKd4X3edSnKOqbbMAEv5j/s400/if_else_ladder.png)
No comments:
Post a Comment