Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

10java Decision Making

Decision making statement

Uploaded by

Sheik Shafi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

10java Decision Making

Decision making statement

Uploaded by

Sheik Shafi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA - DECISION MAKING

http://www.tutorialspoint.com/java/java_decision_making.htm Copyright © tutorials point.com

There are t wo t ypes of decision making st at ement s in Java. They are:

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 {

public static void main(String args[]){


int x = 10;

if( x < 20 ){
System.out.print("This is if statement");
}
}
}

This would produce t he following result :

This is if statement

The if...else St at ement :


An if st at ement can be followed by an opt ional else st at ement , which execut es when t he Boolean
expression is false.

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 {

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");
}
}
}

This would produce t he following result :

This is else statement

The if...else if...else St at ement :


An if st at ement can be followed by an opt ional else if...else st at ement , which is very useful t o t est
various condit ions using single if...else if st at ement .

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 {

public static void 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");
}
}
}

This would produce t he following result :

Value of X is 30

Nest ed if...else St at ement :


It is always legal t o nest if-else st at ement s which means you can use one if or else if st at ement
inside anot her if or else if st at ement .

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 {

public static void main(String args[]){


int x = 30;
int y = 10;

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

This would produce t he following result :

X = 30 and Y = 10

The swit ch St at ement :


A switch st at ement allows a variable t o be t est ed for equalit y against a list of values. Each value is
called a case, and t he variable being swit ched on is checked for each case.

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 following rules apply t o a swit ch st at ement :

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 {

public static void main(String args[]){


//char grade = args[0].charAt(0);
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");
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.

You might also like