1 / 39

The if - else Statements

The if - else Statements. Decision Making in Java. The if - else Statements. if (expression){ statement1 ; }. IF true. If the expression evaluates to true , statement1 is executed. if (expression) { statement1 ; }. If not TRUE. if (expression) { statement1 ; }

dalton-goff
Télécharger la présentation

The if - else Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The if-else Statements Decision Making in Java

  2. The if-else Statements if (expression){ statement1; }

  3. IF true • If the expression evaluates to true, statement1 is executed. if (expression){ statement1; }

  4. If not TRUE if (expression){ statement1; } • . If expression is false then nothing is executed and the program execution picks up after the ending curly brace (}).

  5. for two-way selection • To provide for two-way selection an if statement may add an else option.

  6. for two-way selection • If the expression evaluates to true, the statement is executed. • In an if-else statement, if the expression is false then statement2 would be executed

  7. IF, ELSE • if the expression is false then statement2 would be executed

  8. IF, ELSE • The expression being tested must always be placed in parentheses. ( ) • This is a common source of syntax errors. if(expression){ statement1; }else{ statement2; }

  9. Compound Statements if (expression){ statement1; statement2; statement3; } else{ statement4;

  10. Compound Statements • The statement executed can be a block of statements, grouped together into a single compound statement.{ }

  11. compound statement if (expression){ statement1; statement2; statement3; }else{ statement4; statement5; statement6; } • A compound statement is created by enclosing any number of single statements by braces { }

  12. Nested if-else Statements } if (expression1){ if (expression2){ statement1; }else{ statement2; } } else{ statement3; } } • The statement inside of an if or else option can be another if-else statement.

  13. Braces need Partners • braces will need to be correct to ensure that the ifs and elses get paired with their partners. { }

  14. Who needs braces • Technically, braces are not needed for if and if-else structures • Indentation is ignored by the compiler • However, if you always use braces when writing if and if-else statements, you will never have this problem.

  15. Another alternative • Another alternative is to use of the && or the or | | operator. • A pair of nested if statements can be coded as a single compound && statement.

  16. What type of triangle? • determining the type of triangle given the three sides A, B, and C. • If an equilateral triangle is encountered, the rest of the code is ignored. This can help to reduce the execution time of a program.

  17. if ( (A == B) && (B == C) ) System.out.println("Equilateral triangle"); else if ( (A == B) || (B == C) || (A == C) ) System.out.println("Isosceles triangle"); else System.out.println("Scalene triangle");

  18. Conditional Operator • Java provides an alternate method of coding an if-else statement using the conditional operator. This operator is the only ternary operator in Java, as it requires three operands. The general syntax is: • (condition) ? statement1 : statement2;

  19. Conditional short way • If the condition is true, statement1 is executed. If the condition is false, statement2 is executed.

  20. int max(int a, int b) { // returns the larger of two integers (a > b) ? return a : return b; }

  21. Boolean Identifiers.. . .code that is easier to read The execution of if-else statements depends on the value of the Boolean expression. We can use boolean variables to write code that is easier to read.

  22. boolean variables to write code that is easier to read. • code that reads more like English. if(done == true){ System.out.println("We are done!"); } we can write if(done){ System.out.println("We are done!"); }

  23. Switch Statements switch (expression){ case value1: statement1; statement2; ... break; case value2: statement3; statement4; ... break; case valuen: //any number of case statement may be used statement; statement; break; default: statement; statement; break; } /* end of switch statement */

  24. switch statement • The switch statement attempts to match the integer value of the expression with one of the case values. case value2: statement3; statement4; ... break;

  25. If a match occurs, then all statements past the case value are executed until a break statement is encountered. • The effect of the break statement causes program control to jump to the end of the switch statement. No other cases are executed.

  26. End with a default default: statement; statement; break;

  27. A very common error • when coding a switch do NOT forget to include the break statements to terminate each case value. • If the break statement is omitted, all the statements following the matching case value are executed.

  28. Default needs a break int i = 4; switch (i) { case 1: System.out.println(“Apple”); break; default: System.out.println(“Orange”); case 2: System.out.println(“Banana”); break; } Orange Banana

  29. Default can be anywhere • Note that the default statement can actually be placed anywhere.

  30. printing the work day of the week switch (day){ case 1: System.out.println ("Monday"); break; case 2: System.out.println ("Tuesday"); break; case 3: System.out.println ("Wednesday"); break; case 4: System.out.println ("Thursday"); break; case 5: System.out.println ("Friday"); break; default: System.out.println ("not a valid day"); break; }

  31. count the occurrences of vowels and consonants if (('a' <= letter) && (letter <= 'z')){ switch (letter){ case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : vowel++; break; default : consonant++; break; } }

  32. It is good programming practice to include a break statement at the end of the switch structure.

  33. Switch of If,Else ???? • There are situations where the switch statement should not replace an if-else chain. • If the value being compared must fit in a range of values, the if-else statement should be used.

  34. Use If, Else • If the value being compared must fit in a range of values, the if-else statement should be used.

  35. You got an 88 if(score >= 90 && score <= 100){ grade = 'A'; }else if{(score >= 80 && score < 90) grade = 'B'; }else if{(score >= 70 && score < 80) grade = 'C'; }

  36. Summary • Control structures are a fundamental part of Java.

  37. summary • You will need to practice control structures in Java to become familiar with what types of situations they are useful in.

  38. summary • Also, Boolean expressions are very useful and should be used whenever appropriate to make coding easier.

More Related