Decision making or Conditional statement is depending on the condition block need to be
executed or not which is decided by condition.
If the condition is "true" statement block will be executed, if condition is "false" then
statement block will not be executed.
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:
If statement is most basic statement of Decision making statement. It tells to program to
execute a certain part of code only if particular condition is true.
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
having multiple statements.
For a single statement, it is not required to specify the body.
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
language if and else are the keyword 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.
Applications of if-else statement,
1. if-else nested statement
2. else-if ladder statement