Your SlideShare is downloading. ×
0
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
itft-Decision making and branching in java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

itft-Decision making and branching in java

461

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

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

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
461
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
34
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Decision Making and Branching In JAVA
  • 2. Decision Making Statements • There are two types of decision making statements in Java. They are:  if statements  switch statements
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

×