the if - else statements decision making in java
TRANSCRIPT
![Page 1: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/1.jpg)
The if-else Statements
Decision Making in Java
![Page 2: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/2.jpg)
The if-else Statements
if (expression){
statement1;
}
![Page 3: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/3.jpg)
IF true
• If the expression evaluates to true, statement1 is executed.
if (expression){
statement1;
}
![Page 4: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/4.jpg)
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 (}).
![Page 5: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/5.jpg)
for two-way selection
• To provide for two-way selection an if statement may add an else option.
![Page 6: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/6.jpg)
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
![Page 7: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/7.jpg)
IF, ELSE
• if the expression is false then statement2 would be executed
![Page 8: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/8.jpg)
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;}
![Page 9: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/9.jpg)
Compound Statements
if (expression){
statement1;
statement2;
statement3;
}
else{
statement4;
![Page 10: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/10.jpg)
Compound Statements
• The statement executed can be a block of statements, grouped together into a single compound statement.{ }
![Page 11: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/11.jpg)
compound statement• A compound
statement is created by enclosing any number of single statements
by braces { }
if (expression){
statement1;
statement2;
statement3;
}else{
statement4;
statement5;
statement6;
}
![Page 12: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/12.jpg)
Nested if-else Statements
• The statement inside of an if or else option can be another if-else statement.
} if (expression1){
if (expression2){
statement1;
}else{
statement2;
}
} else{
statement3;
} }
![Page 13: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/13.jpg)
Braces need Partners
• braces will need to be correct to ensure that
the ifs and elses get paired with their partners.
{ }
![Page 14: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/14.jpg)
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.
![Page 15: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/15.jpg)
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.
![Page 16: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/16.jpg)
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.
![Page 17: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/17.jpg)
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");
![Page 18: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/18.jpg)
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;
![Page 19: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/19.jpg)
Conditional short way
• If the condition is true, statement1 is executed. If the condition is false, statement2 is executed.
![Page 20: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/20.jpg)
int max(int a, int b)
{ // returns the larger of two integers
(a > b) ? return a : return b;
}
![Page 21: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/21.jpg)
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.
![Page 22: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/22.jpg)
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!");
}
![Page 23: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/23.jpg)
Switch Statements switch (expression){ case value1:
statement1;statement2;...break;
case value2:statement3;statement4;...break;
case valuen: //any number of case statement may be usedstatement;statement;break;
default:statement;statement;break;
} /* end of switch statement */
![Page 24: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/24.jpg)
![Page 25: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/25.jpg)
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;
![Page 26: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/26.jpg)
• 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.
![Page 27: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/27.jpg)
End with a default
default:
statement;
statement;
break;
![Page 28: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/28.jpg)
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.
![Page 29: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/29.jpg)
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
![Page 30: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/30.jpg)
Default can be anywhere
• Note that the default statement can actually be placed anywhere.
![Page 31: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/31.jpg)
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;}
![Page 32: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/32.jpg)
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; }
}
![Page 33: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/33.jpg)
• It is good programming practice to include a break statement at the end of the switch structure.
![Page 34: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/34.jpg)
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.
![Page 35: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/35.jpg)
Use If, Else
• If the value being compared must fit in a range of values, the if-else statement should be used.
![Page 36: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/36.jpg)
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';}
![Page 37: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/37.jpg)
Summary
• Control structures are a fundamental part of Java.
![Page 38: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/38.jpg)
summary
• You will need to practice control structures in Java to become familiar with what types of situations they are useful in.
![Page 39: The if - else Statements Decision Making in Java](https://reader033.vdocuments.site/reader033/viewer/2022061609/551b23625503462e578b644f/html5/thumbnails/39.jpg)
summary
• Also, Boolean expressions are very useful and should be used whenever appropriate to make coding easier.