Showing posts with label what is Decision making statement in Java. Show all posts
Showing posts with label what is Decision making statement in Java. Show all posts

Saturday, 1 December 2018

Decision making statement in java with example | what is Decision making statement in Java - Online Help

               Decision making statement is nothing but making a decision by applying some conditions. Decision-making statement is classified into three type. They are:
- Simple if statement
- if-else statement
- nested if else statement
- switch statement

Simple if statement:

Simple if statement is used to execute or skip on one statement or set of statements for a particular condition.The general format is:

if(test condition)
{
statement blocks;
}
next statement;

If the test condition is true, the statement block will be executed, otherwise, execution starts from the next statement.



Sample program:

class ifdemo
{
public static void main(String args[])
{
int mark=70;
if(marks>=70)
{
mark=mark+10;
System.out.println("mark="+mark);
}
}

Explanation:
In the above example, test the mark of the statement. If the student marks is equal to 70 then 10 marks will be added to the mark statement.


if-else statement:

if-else statement is used to execute a group of statements.The general format is:
if(test condition)
{
true block statement(s);
else
false block statement(s);
}
next statement;

(i)If the statement is true then the true block statement will be executed and the control transfer to the next statement.
(ii)If the condition is false then a false block statement will be executed and the control statement is transferred to the next statement.



Sample program:

class bigger
{
public static void main(String args[])
{
int a=10,b=25;
if(a>b)
{
System.out.println("A is bigger than B");
else
System.out.println("B is bigger than A");
}
}
}

The output of this program is:
B is bigger than A

Explanation:
This program test the true value of (a&b), if A is bigger than B, it prints "A is bigger than B".Otherwise, It prints"B is bigger than A".

Nested-if statement:

A nested-if is an if statement that is the target of another if or else. Nested ifs are very common in programming. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.The general format:

if(test_condition1)
{
if(test_condition2)
{
statement block1;
else
statement block2;
}
else
statement block3;
}
next statement;

(i)The computer first evaluates the value of the test condition1.
(ii)If test condition1 is false,the control statement executed the statement block3.
(iii)If the test condition1 is true, the control is transferred to test condition2, if the test condition is true, the statement block1 is executed.
(iv)If the condition2 is false, statement blocks 2 is executed.



Sample program:

import java.util.*;
class nesteddemo
{
public static void main(String args[])
{
int a,b,c;
Scanner s=new Scanner(System.in);
System.out.println("enter 3 values:");
a=s.nextInt();
b=s.nextInt();
c=s.nextInt();
if(a>b && a>c)
System.out.println("A value "+a"is large");
else
{
if(b>c && b>a)
System.out.println("B value "+b" is large");
else
System.out.println("C value "+c" is large");
}
}
}

The output of this coding is:
enter 3 value:
10
15
5
B value 15 is large

Switch statement:

The switch statement allows a number of choices. In java, switch statement check the value of given variable or statement against a list of case value and when the match is found a statement-block of that case is executed. Switch statement is also called as multiway decision Statement. The general format of the switch statement is shown here:
switch(expression)
{
case label1:
statement block1;
Break;
case label2:
statement block2;
break;
---------------
---------------
case label n:
statement block n;
break;
default:
default statement;
break;
}
next statement;

(i)switch,case,default,are the keywords.
(ii)Expression is any valid expression
(iii)Default part is optional



Sample program:
import java.util.*;
class decisiondemo
{
public static void main(String args[])
{
int color;
Scanner s=new Scanner(System.in);
System.out.println("enter the color number[1.red,2.blue,3.green]");
color=s.nextInt();
switch(color)
{
case 1:
System.out.println("red");
break;
case 2:
System.out.println("blue");
break;
case 3:
System.out.println("green");
break;
default:
System.out.println("Invalid number");
}
}
}

The output of this program is:
enter the color number[1.red,2.blue,3.green]
2
blue

enter the color number[1.red,2.blue,3.green]
5
invalid number

enter the color number[1.red,2.blue,3.green]
3
green