Download presentation
Presentation is loading. Please wait.
Published byRaymond Brown Modified over 8 years ago
1
Decision Statements, Short- Circuit Evaluation, Errors
2
Goals and Objectives Relational Operators Equality Operators Logical Operators AND, OR, NOT Truth Table. DeMorgan Law Compound Boolean Expression. Short-Circuit Evaluation. Simple if statement if-else statement Dangling Else Nested if statement Extended if statement switch-case statement Flowcharting
3
Relational Operators OperatorMeaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal
4
Equality Operators Test if two values are equal (==) or not equal (!=) == != Note the equality operator is two equal signs side by side and should not be mistaken for the assignment operator.
5
Logical Operators Produce Boolean results and used for compound expressions ! Not && AND || OR
6
And Truth Table And && Exp1Exp2Result True False TrueFalse
7
Or Truth Table Or || Exp1Exp2Result True FalseTrue FalseTrue False
8
Not Truth Table Not ! ExpResult TrueFalse True
9
DeMorgan’s Law !(p && q) is the same as !p || !q !(p || q) is the same as !p && !q
10
Evaluating expressions with NOT To evaluate expression that has a NOT operator, you need to make the expression mean the exact opposite of the original expression. Example: !(x 4 !( y > 5) y <= 5 !(x == 3) x != 3
11
Compound Boolean Expressions More than one Boolean expression in a single condition. Formed using the logical And ( && ), logical Or ( || ), or logical Not ( ! ) operators.
12
Short-Circuit Evaluation Can occur during and && or || compound boolean expressions. expression1 && expression2 –For an AND operator, if expression1 evaluates to false there is no need to check expression2 because the compound expression will automatically be False. expression1 || expression2 –For an OR operator, if expression1 evaluates to true there is no need to check expression2 because the compound expression will automatically be True.
13
Examples of Short-Circuit Evaluation Example: x= 2 (x > 3 && x <10) x > 3 will evaluate to FALSE No need to check x<10 because the compound expression evaluates to false by the 1 st expression. Example: x = 2 (x > 1 || x < 10) –x > 1 will evaluate to TRUE –No need to check x<10 because the compound expression evaluates to TRUE by the 1 st expression.
14
Short-Circuit Evaluation if( y != 0 && x/y > 3) –x/y will not be check if y = 0 thus not causing an error by dividing by 0.
15
Simple If Statement if(boolean expression or variable) statements; Braces { } are used when there are more than one statements. Example: if(boolean expression or variable) { statement; } If the boolean expression or variable is false the statements between the braces will be skipped.
16
Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5. The if Statement
17
Simple If Statements An if statement lets a program choose whether to execute a particular statement. Indentation is important for style and readability. It shows the relationship between one statement and another. Comparing objects – because objects point to a location in memory, rather than directly storing a value, they cannot be compared using built-in relational operators. Never try to compare floating points. Example double x, y; (x == y). Because of the possibility of roundoff error.
18
The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.
19
if-else Statement The if statement can include an optional else clause that is executed when the if condition evaluates to false. if (boolean expression or variable) { Statements; } else { Statements; } No braces are needed if there is just one statement.
20
If-else Statement An if-else statement tells a program to do one thing if the condition is true and another thing if the condition is false. Block Statements – is code enclosed between braces. Indent both body statements of an if-else statement for style and readability.
21
Dangling Else Occurs when an else follows two if statements. An else clause is always matched to the closest unmatched if that came before it. Use braces to correct problem.
22
Dangling Else Example if(condition1) Statement; if(condition2) Statement; else Statement; Which if statement does the else belongs to?
23
Dangling Else Example Correct way if(condition1) { Statement; if(condition2) Statement; } else Statement;
24
Nested if-else Statements Should be indented to make the logic clear. Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5.
25
Nested If Statements Nested if statements are if statements inside other if statements. The dangling if slide earlier is an example of nested if statement. It is very important to use braces to prevent dangling else statements when creating nested if statements. Indentation is very important and makes it easy to read.
26
Extended If Statements Can replace multiple level of nested if statements. Can replace a series of if statements Make the program run faster
27
if-else if – else Example if(condition1) –Statement1; else if (condition2) –Statement2; else if(condition3) –Statement3; else –Statement4;
28
The if-else if Statement Used to decide among three or more actions. Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently.
29
if-else If one of the if’s or else if’s evaluates to be true the rest are skipped. It is wise to put the condition that occurs the most at the beginning of the if-else if- else statements.
30
Switch-case statements The switch statement is a conditional control structures that uses the result of an expression to determine which statement to execute. switch(condition) { case variable: Statement; break; case variable: Statement; break; default: Statement; break }
31
The switch Statement Used to decide among three or more actions. Uses an expression that evaluates to an integer. The break statement moves program execution to the next statement after the switch. The default code is optional and is executed when none of the previous cases are met: switch (numLegs) { case 2: System.out.println("human"); break; case 4: System.out.println("beast"); break; case 8: System.out.println("insect"); break; default: System.out.println("???"); break; }
32
Switch-case Statement Break statement – is necessary to move program control to the next case. Default statement is optional Condition should be integer or character no floating point. Can have more than one case that represents a statement. (Next Slide)
33
Switch – case Statements switch(condition) { case variable: Statement; break; case variable: Statement; break; default: Statement; break }
34
Comparing Objects & Floating Points When comparing objects, you cannot use the (==) relational operator. You cannot compare floating points using (==) relational operator because it may cause a loss of precision error.
35
Common Errors Semicolon after if statement –if(condition); // will compile Missing Semicolon after statement if(condition) Statement // missing semicolon compiler error else Statement; Omitting Braces if(condition) Statement; // No braces for 2 statements Statement; Dangling Else
36
Flowchart Symbols decision
37
The RPS Flowchart
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.