itft-decision making and branching in java

13
Decision Making and Branching In JAVA

Upload: atul-sehdev

Post on 03-Sep-2014

286 views

Category:

Education


0 download

DESCRIPTION

Decision Making Statements,The if Statement, SIMPLE IF STATEMENT, The If…else Statement, Nesting of IF..Else Statements, THE else if ladder, The Switch Statement, rules apply to a switch statement

TRANSCRIPT

Page 1: itft-Decision making and branching in java

Decision Making and BranchingIn JAVA

Page 2: itft-Decision making and branching in java

Decision Making Statements

• There are two types of decision making statements inJava. They are:

if statements

switch statements

Page 3: itft-Decision making and branching in java

The if Statement

• The if statement is a powerful decision making statement and is used to control the flow of execution of statements. The if statement is Java’s conditional branch statement. It can be used to route program execution through two different paths. Here is the general form of the if statement:

if (condition) statement1;

• the if statement may be implemented in different forms depending on the complexity of condition to be tested.

• Simple if Statement

• If…else Statement

• Nested if..else Statement

• else if ladder

Page 4: itft-Decision making and branching in java

SIMPLE IF STATEMENT

• The general form of a simple if statement is

if(test_expression)

{

Statement_true_block;

} Statement_x;

• The statement block may be a single statement or a group of statement. If the test expression is true then statement_true_block will be executed otherwise it will be skipped.

Page 5: itft-Decision making and branching in java

The If…else Statement

• The If…else Statement is an extension of the simple if statement. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

• Syntax:

if(test expression)

{

True-block statement;

}else

{

False-block statement;

}Statement-x;

Page 6: itft-Decision making and branching in java

Nesting of IF..Else StatementsWhen a series of decision are involved then we may have to use more than one if…else statement in nested form as follows:

The general syntax is

if(test condition1)

{

if(test condition2)

{ Statement1;

}

else

{ Statement2;

}

}

else

{ Statement3;

}Statement-x

Page 7: itft-Decision making and branching in java

THE else if ladder

if(test condition1)

statement1;

else if(test condition2)

Statement2;

else if(test condition3)

statement3;

………….

else if(condition n)

statement n;

else

default statement;

statement x;

Page 8: itft-Decision making and branching in java

The Switch Statement

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.

The syntax 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

}

Page 9: itft-Decision making and branching in java

rules apply to a switch statement

• The variable used in a switch statement can only be a byte, short, int, or char.

• You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.

• The value for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.

• When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

Page 10: itft-Decision making and branching in java

Continued..

• When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

• Not every case needs to contain a break. If no break appears, the flow of control will fall throughto subsequent cases until a break is reached.

• A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Page 11: itft-Decision making and branching in java

The ?; operator

• The value of a variable often depends on whether a particular boolean expression is or is not true and on nothing else. For instance one common operation is setting the value of a variable to the maximum of two quantities.

• In Java you might write

if (a > b) {max = a;

}

else {

max = b;}

Page 12: itft-Decision making and branching in java

The ?; operator

• Setting a single variable to one of two states based on a single condition is such a common use of if-else that a shortcut has been devised for it, the conditional operator, ?;. Using the conditional operator you can rewrite the above example in a single line like this:

max = (a > b) ? a : b;

• (a > b) ? a : b; is an expression which returns one of two values, a or b. The condition, (a > b), is tested. If it is true the first value, a, is returned. If it is false, the second value, b, is returned

Page 13: itft-Decision making and branching in java