JAVA.
Do you wanted to know what is Decision making and branching? what are the types of branching? to know that read the following carefully and completely.
DECISION MAKING AND BRANCHING.
The developer need to control the different section of the code that would be executed depending up on the data being processed.
In order to execute code conditionally, the program should respond to condition. if condition is true the code should be executed. This process is basic of all program.
Java has 3 decision making statement.
- if-statement
- switch case statement.
- conditional statement.
If-statement.
The statement evaluated the expression first, and then depending on expression value the statements are executed. If the expression value is either true or false.
There are further grouped into 4.
- Simple if statement.
- If-else statement.
- Nested if statement.
- If else if statement.
Simple if statement.
First the condition is checked, if the condition is true the following code is executes,if false jumps to the next statement.
Syntax:
if (condition)
{
.....
.....
}
flow chart:
If-else statement:
First the condition is checked, if the condition is true the following code-1 is executes if false code-2 is executes.
Syntax:
if (condition)
{
code-1
}
else
{
code-2
}
Flow chart:
Nested if statement:
First the condition is checked, if the condition is true next if statement condition is checked if that is true code1 is executed else code 2 is executed. if false code3 is executes.
Syntax:
if (condition)
{
if (condition)
{
code-1
}
else
{
code-2
}
}
else
{
code-2
}
flowchart:
elseif statement.
First the condition is checked if true the code is executed, else if false next if condition is checked, in the if condition is true the code is executed, else if false next if condition is checked like ways it continues, finally if noting is true final code is executed
Syntax:
SWITCH CASE STATEMENT.
It is used instead of long if statements. It allows us to execute a particular block of code among many block of code.
syntax;
switch(expression)
{
case constant1:
/* statements */
break;
case constant2:
/* statements */
break;
.
.
.
default:
/* statements */
}
working:
- The expression is evaluated and compared to each case values or Constance.
- If found any match that case values's block of statement is executed and switch case is ended using break keyword.
- If match is not found, default code is executed.
Flow chart:
CONDITIONAL STATEMENT.
A Conditional operator is also called Ternary operator. It is similar to if-else statement but, it takes less space and easy to read than if-else statement.
it is also represented as (?:)
syntax:
variable= expression1? expression2: expression3
expression1 is considered to be the condition, if the condition is true, expression2 is executed and result is found, if the condition is false expression3 is executed and result is found.





Comments
Post a Comment