Decision making Structure
Decision Making Structure:-
It is very common control flow statement in order to apply different conditions to program. If the condition is true, the statements that comes after the condition will be executed. In java there are 4 types of decision making structure. They are:
1)The 'if' statement
2) The 'if-else' statement
3)The 'nested if-else statement
4)The 'switch' statement
1) The 'if' statement
if statement is the most basic decision making structure. This statement tells the computer system to execute the certain block of code only if the condition id true. If the certain code is not true then it skips that code and starts to execute next block of code. Syntax:-
if(<conditional expression>)
{
//Block of code executes if the condition is true.
}
for example:-
if(a>b)
{
System.out.println("largest number is: "+a);
}
2)The 'if-else' statement
It is same as if statement. if statements runs the block of code if it the condition is true, if the condition is false then it runs code of else block. Syntax:-
if(<conditional expression>)
{
//Block of code executes if the condition is true.
}
else
{
//Block of code executes if the condition is false.
}
3)The 'nested if-else' statement
Nested if-else statement are if-else statement inside if-else statement. Nested if-else statement are used when more than two decision to be made. Syntax:-compare three variable.
if (a < b)
if (b < c)
max = c;
else
max = b;
else
if (a < c)
max = c;
else
max = a;
It is very common control flow statement in order to apply different conditions to program. If the condition is true, the statements that comes after the condition will be executed. In java there are 4 types of decision making structure. They are:
1)The 'if' statement
2) The 'if-else' statement
3)The 'nested if-else statement
4)The 'switch' statement
1) The 'if' statement
if statement is the most basic decision making structure. This statement tells the computer system to execute the certain block of code only if the condition id true. If the certain code is not true then it skips that code and starts to execute next block of code. Syntax:-
if(<conditional expression>)
{
//Block of code executes if the condition is true.
}
for example:-
if(a>b)
{
System.out.println("largest number is: "+a);
}
2)The 'if-else' statement
It is same as if statement. if statements runs the block of code if it the condition is true, if the condition is false then it runs code of else block. Syntax:-
if(<conditional expression>)
{
//Block of code executes if the condition is true.
}
else
{
//Block of code executes if the condition is false.
}
3)The 'nested if-else' statement
Nested if-else statement are if-else statement inside if-else statement. Nested if-else statement are used when more than two decision to be made. Syntax:-compare three variable.
if (a < b)
if (b < c)
max = c;
else
max = b;
else
if (a < c)
max = c;
else
max = a;
4)The 'switch' statement
if-else statement can be replace by using switch statement. To use switch statement there must be multiple decision. And according to the need of the condition we can use if. It only can be used to make decision on discrete values. Syntax:-
switch(<input variable>)
{
case 1:
System.out.println("name");
break;
case 2:
System.out.prinltn("address");
break;
default:
System.out.println("input your detail");
}
Comments
Post a Comment