itft-Decision making and branching in java


Published on

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

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

itft-Decision making and branching in java

  1. 1. Decision Making and Branching In JAVA
  2. 2. Decision Making Statements • There are two types of decision making statements in Java. They are:  if statements  switch statements
  3. 3. 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
  4. 4. 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.
  5. 5. 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;
  6. 6. Nesting of IF..Else Statements When 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
  7. 7. 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;
  8. 8. 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 }
  9. 9. 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.
  10. 10. 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.
  11. 11. 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;}
  12. 12. 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