As we know that " if " is a conditional control statement and maximum it will execute once depending upon the condition check. If the condition is true then it will execute once and if false then will not execute at even for a single time.
It is classified into three type i.e
1> if statement (only if)
2> if-else statement
3> if-else-if statement or nested if-else statement
" if " statement
As mentioned above it is a conditional statement and can able to execute the code maximum for one time only. Condition used with " if " statement must return boolean value because the code written inside the if statement will be executed either on true or false condition only. If block can have no statement, one statement or multiple statement as per user requirement.
Syntax for if statement :
if ( condition ){ if ( condition )
// statements // statement ---( Part of if )
} // statement ---( Not a part of if )
for ex-
int i = 1 ;
if ( i > 0 ) { System.out.println(" Output :" + i ) ; }
In the above code ( i > 0) is true so the code written withing {} will be executed once.
Note: Condition may a direct value i.e true or false, or using relational operator which always return boolean value or may be a method which must return boolean value.
Lets discuss some conceptual and useful codes using " if " statement.
if ( 1 > 0 )
int x = 10 ;
Here variable declaration x is a first statement and is a local variable so other statement will never be the part of " if " hence there is no use of initializing and declaring local variable as its first statement because it cannot be used other the if block.
if ( 1 > 0 ) { int x = 10 ; }
Above code is same as previous one but with curly bracket {}. This will compile successfully because curly bracket indicates that there may be chance of declaring multiple statement but in case of above code compile is very sure about that statement.
int var ; int var ;
if ( true ) { var = 90 ; } if ( false ) { var = 90 ; }
System.out.println ( var ); System.out.println ( var );
When we are initializing any uninitialized variable in the if block then the compiler must be sure that in all the condition the variable must be initialized. But here in the 2nd code above var is never be initialized because if will never as condition is false.
So i discussed few code here like this there are multiple so keep experimenting like this.
" if - else " statement
It a type of " if " control statement and here else indicate " if- not " that means if " if " block is false then control will go the else block. And both if and else block can have multiple statements and if we are using only single statement for if and else then there is no need to indicating any curly bracket.
Syntax for if statement :
if ( condition ){ if ( condition )
// statements // statement ( must be a single statement)
} else
else { // statement
// statements
}
" if - else-if " statement ( nested if-else )
if - else - if is otherwise called as nested if-else block because we can have infinite series of if-else block but condition else must be followed for every else, if your code will have any if without else then it will give compilation error that " else without if ". So must take care of if-else combination while using it.
Syntax for if statement :
if (condition ){ // statements }
else if (condition) { // statements }
else if (condition ) { // statements }
else { // statement }
In the if - else - if statement we have multiple with multiple condition and if any the condition matched then the corresponding block will execute and after executing the control will be transferred to the proceeding statement after nested if-else.
Here is some invalid declaration of if-else statement.
if ( true ) { if( true ) if ( true )
else { } else { } // statement
} // statement
else { }
The following above codes will give the compilation error saying " Else without if ".
Here is some of the valid declaration of if-else statement
if ( true ) ; if ( true ) if ( true ) { }
else { } // single statement else if( true )
else
// single statement
It is classified into three type i.e
1> if statement (only if)
2> if-else statement
3> if-else-if statement or nested if-else statement
" if " statement
As mentioned above it is a conditional statement and can able to execute the code maximum for one time only. Condition used with " if " statement must return boolean value because the code written inside the if statement will be executed either on true or false condition only. If block can have no statement, one statement or multiple statement as per user requirement.
Syntax for if statement :
if ( condition ){ if ( condition )
// statements // statement ---( Part of if )
} // statement ---( Not a part of if )
for ex-
int i = 1 ;
if ( i > 0 ) { System.out.println(" Output :" + i ) ; }
In the above code ( i > 0) is true so the code written withing {} will be executed once.
Note: Condition may a direct value i.e true or false, or using relational operator which always return boolean value or may be a method which must return boolean value.
Lets discuss some conceptual and useful codes using " if " statement.
if ( 1 > 0 )
int x = 10 ;
Here variable declaration x is a first statement and is a local variable so other statement will never be the part of " if " hence there is no use of initializing and declaring local variable as its first statement because it cannot be used other the if block.
if ( 1 > 0 ) { int x = 10 ; }
Above code is same as previous one but with curly bracket {}. This will compile successfully because curly bracket indicates that there may be chance of declaring multiple statement but in case of above code compile is very sure about that statement.
int var ; int var ;
if ( true ) { var = 90 ; } if ( false ) { var = 90 ; }
System.out.println ( var ); System.out.println ( var );
When we are initializing any uninitialized variable in the if block then the compiler must be sure that in all the condition the variable must be initialized. But here in the 2nd code above var is never be initialized because if will never as condition is false.
So i discussed few code here like this there are multiple so keep experimenting like this.
" if - else " statement
It a type of " if " control statement and here else indicate " if- not " that means if " if " block is false then control will go the else block. And both if and else block can have multiple statements and if we are using only single statement for if and else then there is no need to indicating any curly bracket.
Syntax for if statement :
if ( condition ){ if ( condition )
// statements // statement ( must be a single statement)
} else
else { // statement
// statements
}
" if - else-if " statement ( nested if-else )
if - else - if is otherwise called as nested if-else block because we can have infinite series of if-else block but condition else must be followed for every else, if your code will have any if without else then it will give compilation error that " else without if ". So must take care of if-else combination while using it.
Syntax for if statement :
if (condition ){ // statements }
else if (condition) { // statements }
else if (condition ) { // statements }
else { // statement }
In the if - else - if statement we have multiple with multiple condition and if any the condition matched then the corresponding block will execute and after executing the control will be transferred to the proceeding statement after nested if-else.
Here is some invalid declaration of if-else statement.
if ( true ) { if( true ) if ( true )
else { } else { } // statement
} // statement
else { }
The following above codes will give the compilation error saying " Else without if ".
Here is some of the valid declaration of if-else statement
if ( true ) ; if ( true ) if ( true ) { }
else { } // single statement else if( true )
else
// single statement
nice article for beginners.thank you.
ReplyDeletejava strings class
java strings class