10java Decision Making
10java Decision Making
if st at ement s
swit ch st at ement s
The if St at ement :
An if st at ement consist s of a Boolean expression followed by one or more st at ement s.
Synt ax:
The synt ax of an if st at ement is:
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
If t he Boolean expression evaluat es t o t rue t hen t he block of code inside t he if st at ement will be
execut ed. If not t he first set of code aft er t he end of t he if st at ement (aft er t he closing curly
brace) will be execut ed.
Example:
public class Test {
if( x < 20 ){
System.out.print("This is if statement");
}
}
}
This is if statement
Synt ax:
The synt ax of an if...else is:
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
Example:
public class Test {
if( x < 20 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
When using if , else if , else st at ement s t here are few point s t o keep in mind.
An if can have zero or one else's and it must come aft er any else if's.
An if can have zero t o many else if's and t hey must come before t he else.
Once an else if succeeds, none of t he remaining else if's or else's will be t est ed.
Synt ax:
The synt ax of an if...else is:
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes 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 {
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");
}
}
}
Value of X is 30
Synt ax:
The synt ax for a nest ed if...else is as follows:
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
You can nest else if...else in t he similar way as we have nest ed if st at ement .
Example:
public class Test {
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
}
X = 30 and Y = 10
Synt ax:
The synt ax of enhanced for loop is:
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
The variable used in a swit ch st at ement can only be a byt e, short , int , or char.
You can have any number of case st at ement s wit hin a swit ch. Each case is followed by t he
value t o be compared t o and a colon.
The value for a case must be t he same dat a t ype as t he variable in t he swit ch and it must be a
const ant or a lit eral.
When t he variable being swit ched on is equal t o a case, t he st at ement s following t hat case will
execut e unt il a break st at ement is reached.
When a break st at ement is reached, t he swit ch t erminat es, and t he flow of cont rol jumps t o
t he next line following t he swit ch st at ement .
Not every case needs t o cont ain a break. If no break appears, t he flow of cont rol will fall
through t o subsequent cases unt il a break is reached.
A switch st at ement can have an opt ional default case, which must appear at t he end of t he
swit ch. The default case can be used for performing a t ask when none of t he cases is t rue. No
break is needed in t he default case.
Example:
public class Test {
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");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
Compile and run above program using various command line argument s. This would produce t he
following result :
$ java Test
Well done
Your grade is a C
$
What is Next ?
Next chapt er discuses about t he Number class (in t he java.lang package) and it s subclasses in Java
Language.
We will be looking int o some of t he sit uat ions where you would use inst ant iat ions of t hese classes
rat her t han t he primit ive dat a t ypes, as well as classes such as format t ing, mat hemat ical funct ions
t hat you need t o know about when working wit h Numbers.