• +1-617-874-1011 (US)
  • +44-117-230-1145 (UK)
Live Chat
Follow Us:

Java Decision Making Homework Help

Java Decision Making

The decision making structures is used to check the condition and produces Boolean based result depends it statement will execute. Java has following types of decision making statements.

1. If statement

2. If…else statement

3. Nested if statement

4. Switch statement

If statement:

It is executed based on Boolean statement.

Syntax:

If (condition)

{

Statements;

}

Example:

public class sample {
 public static void main(String args[])
 {
 int i = 20;
 if( i < 30 )
 {
 System.out.print("if statement");
 }
 }
}

Output:

if statement

if…else statement:

it can follow by else statement. When the Boolean statement false it will be executed.

Syntax:

If (condition)

{

Statements;

}

else

{

Statement;

}

Example:

public class sample {
 public static void main(String args[])
 {
 int i = 40;
 if( i < 30 )
 {
 System.out.print("if statement");
 }
else
{
 System.out.print("else statement");
}
 }
}

Output:

else statement

if …else if statement:

it can follow by else if statement. When the Boolean statement false it will be again check another condition and executed.

Syntax:

If (condition)

{

Statements;

}

else if(condition)

{

Statement;

}

else

{

Statement;

}

Example:

public class sample {
 public static void main(String args[])
 {
 int i = 40;
 if( i == 30 )
 {
 System.out.print(" I = 30");
 }
else (i==40)
{
 System.out.print("I = 40");
}
else
{
 System.out.print("no value");
}
 }
}

Output:

I = 40

Nested if statement:

The want to check more than one condition means we use nested if that if with if statement.

Syntax:

If (condition1)

{

If(condition2)

{

Statement

}

}

Example:

public class sample
 {
 public static void main(String args[])
{
 int i = 40;
 int j = 20;
 if( i == 40 )
{
 if( j == 20 )
{
System.out.print("i = 40 and j = 20");
 }
 }
 }
}

Output:

i = 40 and j = 20

Switch Statement:

It is used to test the list of values. The variable is checked for each case.

Syntax:

Switch(expression)

{

Case value:

Statements

Break; (optional)

Case value:

Statements

Break; (optional)

Default: (optional)

Statements

}

Example:

public class sample {
 public static void main(String args[])
{
 int mark = '75';
 switch(mark)
{
 case '75' :
System.out.println("first class");
break;
 case '65' :
 case '60' :
System.out.println("second class");
break;
 default :
System.out.println("Invalid mark");
 }
 System.out.println("Your mark is " + mark);
 }
}

Output:

first class

Your mark is 75