If, If-Else and It Applications
If, If-Else and It Applications
If the condition is "true" statement block will be executed, if condition is "false" then
In this section we are discuss about if-then (if), if-then-else (if-else), and switch statement.
In Java language there are three types of decision making statements we have.
1. if
2. if-else
3. switch
if Statement:
Syntax:
if(condition/expression)
statement-1;
statement-2;
statement-3;
statement-n;
}
Constructing the body of "if" statement is always optional, Create the body when we are
If the body is not specified, then automatically condition part will be terminated with next
semicolon (;).
else statement:
It is a keyword, by using this keyword we can create an alternative block for "if" part. Using
else is always optional i.e, it is recommended to use when we are having alternate block of
condition.
In any program among if and else only one block will be executed. When if condition is
false then else part will be executed, if part is executed then automatically else part will be
ignored.
if-else statement:
In general it can be used to execute one block of statement among two blocks, in java
Syntax:
if(Condition)
statement-1;
statement-2;
statement-3;
statement-n;
else
statement-1;
statement-2;
statement-3;
statement-n;
In the above syntax whenever condition is true all the if block statement are executed
remaining statement of the program by neglecting else block statement.
If the condition is false else block statement remaining statement of the program are
executed by neglecting if block statements.