Java Tut – Decision Making

If statement:

if(boolean_expression)
{
//Statement will execute if the boolean expression is true
}

e.g:

public class Test{
public static void main(String[] args)
{
int x=10;
if(x<20){
System.out.print("This is if statement");
}
}
}

output:

This is if statement.

If-else statement:

Syntax:
if(Boolean_expression)
{
//Executes when the boolean expression is true
}else{
//executes when the boolean expression is false
}

example:

public class Test{

public static void main(String[] args)
{
int x=30;
if(x<20)
{
System.out.print("This is if statement");
}else
{
System.out.print("This is else statement");
}
}
}

output:

This is else statement

if..else if…else statement:
Syntax:

if(Boolean_expression 1)
{
//Executes when the Boolean expression 1 is true
}
else if(Boolean_expression 2)
{
//Execute when the boolean expression 2 is true
}else if(Boolean_expression 3)
{
//Executes when the boolean expression 3 is true
}
else
{
//Executes when the none of the above condition is true
}

example:

public class Test{
public static vodi main(String[] args)
{
int x=30;

if(x==10)
{
System.out.print("Value of x is 10");
}else if(x==20)
{
System.out.print("Value of x is 20");
}else if(x==30)
{
System.out.print("Value of x is 30");
}else
{
System.out.print("This is else statement");
}
}
}

output:

Value of x is 30

nested if statement in java:
It is alwys legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statements.

if(boolean_expression 1)
{
//executes when the boolean expression 1 is true
if(boolean_expression 2)
{
//executes when the boolean expression 2 is true
}
}

we can also nest else if…else in the similar way as we have nested the if statement

public class Nest{
public static void main(String[] args)
{
int x=30;
int y=10;

if(x==30)
{
if(y==10)
{
System.out.print("Hei x=30 and Y =10 :D");
}
}
}
}

output:

Hei x=30 and Y =10 :D

Switch statements in Java
A switch statement allows a variable to be tested for equality against a list of values.Each value is called a case and the variable being switched on is checked for each case.
Syntax:

switch(expression){
case value:
//Statements
break;//optional

case value:
//Statements
break;//optional
//You can have any number of case statements
default: //Optional
//statements

}

example:

public class Switch {
public static void main(String[] args)
{
	char grade='C';
	
	switch(grade)
	{
	case 'A':
		System.out.println("Excellent!");
		break;
	case 'B':
	case 'C':
		System.out.println("Well Done!");
		break;
	case 'D':
		System.out.println("You passed");
		break;
	case 'F':
		System.out.println("Better try again!");
		break;
	default:
			System.out.println("Invalid grade");
}
	System.out.println("Your grade is :"+grade);
}
}

output:

Well Done!
Your grade is :C

 

The ? : Operator
it is known as conditional operator in some previous post i have shown it.It can repalce the if…else statements.
Syntax:
exp1?exp2:exp3;

where exp1,exp2 and exp3 are expressions, initially exp1 is evaluated,
– If the value of exp1 is true, then the vaue of exp2 will be the value whole expression
– If the value exp1 is false, then exp3 is evaluated and its value becomes the value of the entire expression.

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.