Docsity

Java Decision Making: if and switch Statements, Summaries of Computer Science

Toggle sidebar
bg1
http://www.tutorialspoint.com/java/java_d ecision_making.htm Copyright Ā© tutorials point.com
JAVA - DECISION MAKING
There are two types of decision making st at ement s in Java. They are:
if st at ements
switch stat ements
The if Statement:
An if statement consists of a Boolean expression followed by one or more statements .
Syntax:
The syntax of an if statement is:
if(Boolean_expression)
{
//Statements will execute if the Boolean expression is true
}
If the Boolean express ion evaluates t o t rue then the block of code inside the if st at ement will be
execut ed. If not t he first set of code after the end of the if st at ement (aft er the closing curly
brace) will be executed.
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 statement can be followed by an optional else st atement, which execut es when the Boolean
expression is false.
Syntax:
The syntax 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[]){
pf3
pf4

Partial preview of the text

Download Java Decision Making: if and switch Statements and more Summaries Computer Science in PDF only on Docsity!

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

JAVA - DECISION MAKING

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

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.