Complete the chapter assignments below in accordance with the instructions
For every programming tasks, kindly supply the entire source code, take screenshots of the code as it appears in your Eclipse IDE, and send photos of the results after it is executed. Remember to add captions to every screenshot.
Question starts page 73
Chapter 3 Lecture Notes
THE IF STATEMENT
The if statement in Java is a conditional control structure that is used
to execute a block of code only when a specified condition is true. It
helps in making decisions within a Java program.
Syntax:
Here is the basic syntax for an if statement in Java:
java
Copy code
if (condition) {
// code to be executed if the condition is true
}
Example:
java
Copy code
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 5) {
System.out.println(“The number is greater than 5.”);
}
}
}
In the above example, the condition number > 5 is evaluated. Since number
is 10 and the condition is true, the code inside the if block will be
executed, and “The number is greater than 5.” will be printed to the
console.
Extended Variants:
You can also extend if statements with else if and else blocks to handle
multiple conditions:
java
Copy code
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else {
// code to be executed if both condition1 and condition2 are false
}
By using the if, else if, and else statements, you can control the flow
of your Java program based on different conditions.
BOOLEAN EXPRESSIONS
In Java, a Boolean expression is a statement that evaluates to either
true or false. Boolean expressions are commonly used in control flow
statements like if, while, and for to determine the execution path of a
program. The primary relational and logical operators used to form
Boolean expressions in Java are as follows:
Relational Operators:
==: Equal to
!=: Not equal to
: Greater than
=: Greater than or equal to
Logical Operators:
&&: Logical AND
||: Logical OR
!: Logical NOT
Examples of Boolean Expressions:
Here are some examples that demonstrate the use of Boolean expressions in
Java:
java
Copy code
int x = 5;
int y = 10;
boolean isEqual = (x == y); // Evaluates to false
boolean isNotEqual = (x != y); // Evaluates to true
boolean isGreater = (x > y); // Evaluates to false
boolean isLess = (x < y); // Evaluates to true
boolean complexExpression = (x < y) && (y > 0); // Evaluates to true
Using Boolean Expressions in Control Flow:
Boolean expressions are frequently used in if, else if, and else
statements, as well as while and for loops.
If Statement:
java
Copy code
if (x < y) {
System.out.println("x is less than y.");
}
While Loop:
java
Copy code
while (x < y) {
x++;
}
For Loop:
java
Copy code
for (int i = 0; i < 10 && i != x; i++) {
// Do something
}
Boolean expressions are integral to controlling the flow of a Java
program, allowing for more dynamic and adaptable code.
THE ELSE CONDITION
In Java, the else condition is used in conjunction with the if statement
to specify a block of code that should be executed if the condition
specified in the if statement is false. The else block follows the if
block and is optional. It provides an alternative pathway for the code
execution if the initial if condition is not met.
Syntax:
Here's the basic syntax for using else with an if statement:
java
Copy code
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
Here's a simple example that uses if and else:
java
Copy code
int number = 7;
if (number > 10) {
System.out.println(“The number is greater than 10.”);
} else {
System.out.println(“The number is 10 or less.”);
}
In this example, since number is 7 and not greater than 10, the else
block will be executed, and “The number is 10 or less.” will be printed
to the console.
Extended Use:
You can also use else in combination with else if to handle multiple
conditions:
java
Copy code
int score = 75;
if (score > 90) {
System.out.println(“Excellent!”);
} else if (score > 80) {
System.out.println(“Very Good!”);
} else if (score > 70) {
System.out.println(“Good!”);
} else {
System.out.println(“Needs Improvement.”);
}
In this extended example, since the score is 75, the program will print
“Good!” to the console. The else block serves as a fallback for all the
conditions that aren’t met by the if and else if clauses.
The else condition serves an important role in decision-making structures
in Java, allowing you to create more versatile and responsive programs.
STRINGS AND CONDITIONAL EXPRESSIONS
In Java, strings and conditional expressions often go hand-in-hand to
facilitate more dynamic and functional programs. You might need to
compare strings to make decisions, or use conditional expressions to
dynamically create or manipulate strings. Here’s how you can utilize them
together:
Comparing Strings:
When comparing strings, it’s important to remember that == checks if two
strings reference the same object, which is not usually what you want.
Instead, you should use .equals() for value equality.
java
Copy code
String str1 = “hello”;
String str2 = “world”;
String str3 = “hello”;
if (str1.equals(str2)) {
System.out.println(“str1 and str2 are equal.”);
} else {
System.out.println(“str1 and str2 are not equal.”);
}
if (str1.equals(str3)) {
System.out.println(“str1 and str3 are equal.”);
}
Conditional String Creation:
You can also create strings based on conditions. For example:
java
Copy code
boolean isTrue = true;
String message = isTrue ? “This is true.” : “This is false.”;
System.out.println(message);
Using Strings in Switch Statements:
Java also allows strings to be used in switch statements:
java
Copy code
String day = “Monday”;
switch (day) {
case “Monday”:
System.out.println(“Start of the work week”);
break;
case “Wednesday”:
System.out.println(“Hump day”);
break;
case “Friday”:
System.out.println(“End of the work week”);
break;
default:
System.out.println(“Regular day”);
}
String Manipulation Based on Conditions:
Sometimes, you might need to alter a string based on some condition:
java
Copy code
String original = “Java”;
String modified = “”;
if (original.length() > 2) {
modified = original.substring(0, 2).toUpperCase();
} else {
modified = original.toLowerCase();
}
System.out.println(“Modified string: ” + modified);
In the above example, the string “Modified string: JA” will be printed
because the original string “Java” has more than two characters.
Combining Multiple Conditions:
You can combine multiple conditions to make more complex decisions
involving strings.
java
Copy code
String username = “admin”;
String password = “1234”;
if (username.equals(“admin”) && password.equals(“1234”)) {
System.out.println(“Access granted.”);
} else {
System.out.println(“Access denied.”);
}
Strings and conditional expressions in Java can be powerful when used
together, offering various ways to control flow and manipulate data in
your program.
VALIDATING INPUT
Validating input is a crucial practice in programming to ensure that a
program behaves as expected and can handle a variety of situations
gracefully. This is especially important when the input is coming from an
external source, like user input from a console, a web form, or a file.
Java offers several ways to validate input, such as conditional
statements, regular expressions, and even external libraries. Below are
some examples and techniques for input validation in Java.
Using Conditional Statements:
You can use if, else if, and else statements to validate input. For
example, if you are expecting a number to be in a specific range:
java
Copy code
int inputNumber = 45; // Assume this value comes from the user
if (inputNumber >= 1 && inputNumber 0 && y > 0) {
// true because both x and y are greater than 0
System.out.println(“Both x and y are positive numbers.”);
}
// Combining Both
if (x > 0 && y > x) {
// true because x is greater than 0 and y is greater than x
System.out.println(“x is a positive number and y is greater than x.”);
}
// Using Logical NOT
if (!(x == y)) {
// true because x is not equal to y
System.out.println(“x and y are not equal.”);
}
Nested and Chained Conditions
You can nest or chain conditions for more complexity:
java
Copy code
// Nested conditions
if (x > 0) {
if (y > x) {
System.out.println(“y is greater than x, and x is positive.”);
}
}
// Chained conditions using else if
if (x > y) {
System.out.println(“x is greater than y.”);
} else if (x < y) {
System.out.println("x is less than y.");
} else {
System.out.println("x and y are equal.");
}
Understanding the interplay between Boolean logic and relational
operators will help you write more robust and efficient code. These
concepts form the basis of decision-making and control flow in Java, as
well as many other programming languages.
BOOLEAN VARIABLES
In Java, a Boolean variable can have one of two possible values: true or
false. Boolean variables are commonly used to control the flow of a
program using conditional statements like if, while, and for. They're
also used to keep track of conditions; for example, whether a user is
logged in or not.
Declaration and Initialization
You can declare a Boolean variable using the boolean keyword. Here's how
you can declare and initialize a Boolean variable:
java
Copy code
boolean isTrue = true;
boolean isFalse = false;
Using Boolean Variables in Conditions
Boolean variables are often used in if statements and loops:
java
Copy code
boolean isValid = true;
if (isValid) {
System.out.println("The operation is valid.");
}
You can also use Boolean variables in while loops:
java
Copy code
boolean keepRunning = true;
while (keepRunning) {
// some code here
// condition to stop the loop
if (someCondition) {
keepRunning = false;
}
}
Boolean Variables in Expressions
Boolean variables can be combined with logical operators to form more
complex expressions:
java
Copy code
boolean isAdult = true;
boolean hasTicket = false;
if (isAdult && hasTicket) {
System.out.println("Access granted.");
} else {
System.out.println("Access denied.");
}
Modifying Boolean Variables
You can also modify the value of a Boolean variable during the execution
of a program:
java
Copy code
boolean isLocked = false;
// Some code that changes the value
isLocked = true;
if (isLocked) {
System.out.println("The door is locked.");
}
Methods Returning Boolean Values
It's common to have methods that return Boolean values, often used to
check some conditions:
java
Copy code
public boolean isEven(int number) {
return number % 2 == 0;
}
// Usage
boolean result = isEven(4); // This will set result to true
In summary, Boolean variables are incredibly versatile and useful for
controlling the flow of a program, validating conditions, and toggling
states. Understanding how to use them effectively is a fundamental
programming skill.
Introduction to Java
Chapter 3
Decision Structures and Boolean Logic
Introduction to Java: Chapter 3 Lecture Slides by Chris Simber are licensed under
a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.
All screenshots included on the basis of Fair Use.
Decision Structures and Boolean Logic
• Decisions Structures
– Determine the statements that will execute based upon a
condition
– A conditional expression is used to determine whether or
not a line or lines of code execute
– They provide multiple paths through a program based on
the status of a Boolean (true or false) condition
– If the condition is true, then a statement or statements
are executed, otherwise they are not executed
Chris Simber 2023 - OER Text Resource
2
Decision Structures and Boolean Logic
• Decisions Structures
– The decision structure is implemented using the if
statement which is typically referred to as an if clause
• Assume a Theater has seating for 400 participants.
• Once the Theater has sold 400 tickets, the show has been
sold out
• When this occurs, the Theater displays a “Sold Out” sign
• The decision to display the sign is made based upon
whether or not 400 tickets have been sold
Chris Simber 2023 - OER Text Resource
3
Decision Structures and Boolean Logic
• Decisions Structures
– The condition is a test to determine if 400 tickets been
sold, and if it is true, the “Sold Out” sign is displayed.
– If the condition is false and 400 tickets have not been
sold, then the sign will not be displayed
Chris Simber 2023 - OER Text Resource
4
Decision Structures and Boolean Logic
• Decisions Structures
– Conditional expressions are
represented in flowcharts as
diamonds
– The different paths that the
program can take are shown
using lines from the corners of
the diamond, arrows indicate
the direction, and text
indicates the result
Chris Simber 2023 - OER Text Resource
5
Decision Structures and Boolean Logic
• Decisions Structures
– These paths in the program
are often referred to as the
Flow of Control or the Order
of Operations
– If the condition is true, then
the flow of control follows
the path to display the sign
– If the condition is false, the
program continues
Chris Simber 2023 - OER Text Resource
6
Decision Structures and Boolean Logic
• The if Condition
– When programming a conditional expression in Java, the
syntax includes parenthesis around the conditional
expression, and braces to enclose the statement(s)
executed when the condition is true.
No semicolon
Chris Simber 2023 - OER Text Resource
7
Decision Structures and Boolean Logic
• The if Condition
– The statements to be executed are indented for
readability (most IDEs will automatically indent these
lines).
– Some Java developers place the opening brace on a
separate line following the conditional expression,
however most Java Style Guides prefer the format shown
here
Chris Simber 2023 - OER Text Resource
8
Decision Structures and Boolean Logic
• The if Condition
– The Theater example would test whether 400 tickets have
been sold and display “Sold Out” if it is true
Chris Simber 2023 - OER Text Resource
9
Decision Structures and Boolean Logic
• Decisions Structures
– When multiple statements are associated with a
condition, they form what is commonly referred to as a
block of code
– A block of code is a group of associated statements
typically enclosed in braces
Chris Simber 2023 - OER Text Resource
10
Decision Structures and Boolean Logic
• Decisions Structures
– If the condition is true, all of the statements (the block of
code) within the braces will be executed
– If the condition is false, all of the statements within the
braces (the block of code) will be skipped
Chris Simber 2023 - OER Text Resource
11
Decision Structures and Boolean Logic
• Decisions Structures
– Assume that when the theater show is sold out, in
addition to the sold out sign being displayed, the box
office is closed
– A standard practice is to indent in pseudocode just as the lines
would be indented in the actual code
Chris Simber 2023 - OER Text Resource
12
Decision Structures and Boolean Logic
• Decisions Structures
– The flowchart would be modified to include the added
operation.
Chris Simber 2023 - OER Text Resource
13
Decision Structures and Boolean Logic
• Decisions Structures
– The code would be modified to include the added
operation
Chris Simber 2023 - OER Text Resource
14
Decision Structures and Boolean Logic
• Boolean Expressions
– Conditional expressions are either true or false (Boolean)
– Named after the mathematician George Boole (18151864)
– Boolean expressions are implemented using Relational
Operators that resolve to either true or false by testing
relationships
Boolean expressions are either true or false
Chris Simber 2023 - OER Text Resource
15
Decision Structures and Boolean Logic
• Boolean Expressions
– The result of the expression determines the next step or
path for the program
– For example, one value can be greater than another, or
less than another, or equal to another
• One of these three cases must be true, and the others
would be false
Chris Simber 2023 - OER Text Resource
16
Decision Structures and Boolean Logic
• Boolean Operators
– Note that two equal signs are used to test for equivalence
(a single equal sign is the assignment operator)
Chris Simber 2023 - OER Text Resource
17
Decision Structures and Boolean Logic
• Boolean Operators
– Examples
Chris Simber 2023 - OER Text Resource
18
Decision Structures and Boolean Logic
• The else Condition
– The Theater example conditionally displays “Sold Out”
and “Box Office Closed” if exactly 400 tickets have been
sold
• Otherwise the program does nothing
– To provide for other statements to execute when the
condition is false, an else clause is implemented which
can be thought of as an “otherwise” condition for when
the relational expression is not true
Chris Simber 2023 - OER Text Resource
19
Decision Structures and Boolean Logic
• The else Condition
– When the “if” condition is true, the statements in the “if”
block will be executed and the “else” block will be
skipped
– When the “if” condition is false, the “if” block will be
skipped and the “else” block will execute
Chris Simber 2023 - OER Text Resource
20
Decision Structures and Boolean Logic
• The else Condition
– Continuing the Theater example, if 400 tickets have not
been sold, tickets will continue to be sold
Chris Simber 2023 - OER Text Resource
21
Decision Structures and Boolean Logic
• The else Condition
– A flowchart highlights the two paths taken as a result of the
conditional expression, and that only one path will be executed
Chris Simber 2023 - OER Text Resource
22
Decision Structures and Boolean Logic
• The else Condition
– The modified program.
Chris Simber 2023 - OER Text Resource
23
Decision Structures and Boolean Logic
• The Nested if Structure
– When two (or more) conditions are being tested, there
are several ways to implement the logic
– One of these is to use a nested if, which is an “if”
condition inside another “if” condition
Chris Simber 2023 - OER Text Resource
24
Decision Structures and Boolean Logic
• The Nested if Structure
– If condition1 below is true, then condition2 is tested, and if it is
also true, then the statements will be executed.
– If condition1 is false, then condition2 will not be tested and the
statements are skipped
Chris Simber 2023 - OER Text Resource
25
Decision Structures and Boolean Logic
• The Nested if Structure
– The Theater example now includes a balcony section in
addition to the main-floor seats
– The tickets sales are tracked separately for each floor
– To determine if the show is sold out requires testing both
areas separately for the tickets sold
Chris Simber 2023 - OER Text Resource
26
Decision Structures and Boolean Logic
• The Nested if Structure
Chris Simber 2023 - OER Text Resource
27
Decision Structures and Boolean Logic
• The if, else if, else Structure
– To handle situations where multiple conditions result in
different paths, an “if” and an “else” are inadequate
because there are only two paths
– Additional “if” statements may be appropriate, but more
often the if, else-if, else structure is a better solution
Chris Simber 2023 - OER Text Resource
28
Decision Structures and Boolean Logic
• The if, else if, else Structure
– Note that else-if is not an otherwise condition, but
another conditional test that is only tested if the test
before it is false
– Once a condition is found to be true, the others are
skipped over
– The else clause handles the situation when none of the
conditions is true
Chris Simber 2023 - OER Text Resource
29
Decision Structures and Boolean Logic
• The if, else if, else Structure
Chris Simber 2023 - OER Text Resource
30
Decision Structures and Boolean Logic
• Designing Conditional Structures
Any grade above 60 in the code on the left is a “D”
Chris Simber 2023 - OER Text Resource
31
Decision Structures and Boolean Logic
• Designing Conditional Expressions
– Designing conditional expressions for computer programs
is an important skill to develop
– Many difficult-to-find bugs are caused by incorrect
conditional expressions and relational evaluations, and
the use of break and continue which bypass conditional
logic
– Pseudocode and flowcharts are helpful design tools, and
careful testing during development can eliminate most
bugs
Chris Simber 2023 - OER Text Resource
32
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– When a password is created or changed it is typically
entered twice to confirm
– The two are compared to ensure that they match
– In computing, what is actually being compared is the
ASCII representation of the letters character by character
– Recall from Chapter 1 that each character has a binary
representation in the ASCII character set (Appendix A)
Chris Simber 2023 - OER Text Resource
33
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– To compare Strings in Java, the equivalence operator
cannot be used (it would compare the locations of the
strings in memory)
– The member function String.equals() is used
Chris Simber 2023 - OER Text Resource
34
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– To compare Plan and Play, the ASCII values for the letters
are 110 for ‘n’ and 121 for ‘y’
Chris Simber 2023 - OER Text Resource
35
Decision Structures and Boolean Logic
• Strings and Conditional Expressions
– Therefore, they are not equal since ‘y’ has a different
ASCII value than ‘n’
– In fact, Play would be greater than Plan because of the
ASCII value being higher
Play > Plan
True
Characters are compared using their ASCII value
Chris Simber 2023 – OER Text Resource
36
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There is no guarantee that a user will always enter
numeric values when required
– The methods nextInt() and nextDouble() will fail if the
value is not the correct data type
– One way to resolve this is by first reading the input as a
String and then parsing the input to the data type desired
A user may not enter a numeric value as requested
Chris Simber 2023 – OER Text Resource
37
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There are two methods for converting to numeric values
– The method Integer.parseInt() converts the data type
from a string to an integer
Chris Simber 2023 – OER Text Resource
38
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The method Double.parseDouble() converts the string to
a double
Chris Simber 2023 – OER Text Resource
39
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The code below fails and does not complete because the
parsing attempt fails
– The String cannot be parsed to an integer
Chris Simber 2023 – OER Text Resource
40
Decision Structures and Boolean Logic
• String to Numeric Conversion
– The method hasNextInt() returns a Boolean value based
on the next input…it looks ahead
Chris Simber 2023 – OER Text Resource
41
Decision Structures and Boolean Logic
• String to Numeric Conversion
– There are look-ahead methods for double, a line, and
anything
– The method hasNextDouble() will check for a double
– The method hasNext() will check for anything
– The method hasNextLine() will check for a line
Chris Simber 2023 – OER Text Resource
42
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– When a program needs to make decisions based on
complex conditions, multiple conditions can be combined
using the Logical Operators
“&&” for AND
“||” for OR
“!” for NOT
Relational Operators combine conditions
Chris Simber 2023 – OER Text Resource
43
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– With a logical AND, both sides of the expression must be
true for the condition to be true
– If either of them is false, then the expression is false
Both conditions must be true for the expression to be true
Chris Simber 2023 – OER Text Resource
44
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND truth table
Chris Simber 2023 – OER Text Resource
45
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND is often used to verify that a number is
within a range
– If the program requires a number between 0 and 10, the
conditions can be combined in a single expression
– This is referred to as range checking
– Both conditions must be true
Logical AND can be used for numeric range checking
Chris Simber 2023 – OER Text Resource
46
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Logical AND used to verify a number is within a range
Chris Simber 2023 – OER Text Resource
47
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The Theater Ticket Sales example required main-floor
seats and balcony seats to be sold out for the Theater to
display the “Sold Out” sign and close the box office
– Both conditions must be true and a nested “if” condition
was used in the example
Chris Simber 2023 – OER Text Resource
48
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The Logical AND combines the conditions
Both conditions must be true for the expression to be true
Chris Simber 2023 – OER Text Resource
49
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The logical OR is used to test an either-or condition
– If either of the conditions is true, then the expressions is
true
With logical OR, if either condition is true the expression is true
Chris Simber 2023 – OER Text Resource
50
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– The logical OR truth table
Chris Simber 2023 – OER Text Resource
51
Decision Structures and Boolean Logic
• Boolean Logic – Relational Operators
– Converting the previous example with logical OR tests
outside the range instead of inside
– If either condition is true, then the expression is true, and
the number is not within the range required
Chris Simber 2023 – OER Text Resource
52
Decision Structures and Boolean Logic
• Short-circuit Evaluation
– With the logical AND, both sides of the compound
condition must be true for the expression to be true
– So, if the left side is false then the right side is not
evaluated
– It wouldn’t matter if the right side were true since the
expression is already false
Logical AND short-circuit evaluation
Chris Simber 2023 – OER Text Resource
53
Decision Structures and Boolean Logic
• Short-circuit Evaluation
– With the logical OR, when either side is true the
expressions is true
– So, if the left side is true then the right side is not
evaluated
– It wouldn’t matter if the right side were false since the
expression is already true
Logical OR short-circuit evaluation
Chris Simber 2023 – OER Text Resource
54
Decision Structures and Boolean Logic
• Common Logic Errors
– Logic errors are those errors that do not halt execution of
the program but produce incorrect results
– Many of these occur due to incorrect logical expressions
– Pseudocode and flowcharts can help, but careful
consideration of the logic is required
Logic errors produce incorrect results
Chris Simber 2023 – OER Text Resource
55
Decision Structures and Boolean Logic
• Common Logic Errors
– Note the results in these examples
Chris Simber 2023 – OER Text Resource
56
Decision Structures and Boolean Logic
• Boolean Variables
– Variables that can only have a value of true or false
• The computer actually stores these as 1 (true) or 0 (false)
– They are often used to store the result of a condition, or
as a flag that is set as the result of some processing
Boolean variables can only be true or false
Chris Simber 2023 – OER Text Resource
57
Decision Structures and Boolean Logic
• Boolean Variables
– Below a Boolean variable is set to false and if the
condition is true, the variable is flipped to true
Chris Simber 2023 – OER Text Resource
58
Decision Structures and Boolean Logic
• Boolean Variables
– The variable is then used to determine the next step in
the program
Chris Simber 2023 – OER Text Resource
59
Decision Structures and Boolean Logic
• Boolean Variables
– Some programmers prefer to write out the conditional
expression for clarity
• The expressions are equivalent
– The expression on the left is preferred
Chris Simber 2023 – OER Text Resource
60
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– Returns the opposite of a Boolean expression or operand
– If the operand or expression is true, the NOT operator
returns false
– If the operand is false, the NOT operator returns true
Chris Simber 2023 – OER Text Resource
61
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– Use caution when using the NOT operator
– It often introduces bugs and confusion
– This expression reads “If x is greater than y is true, and x
is greater than z is true, then return false”
– It isn’t clear what condition is being tested
Chris Simber 2023 – OER Text Resource
62
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– It is usually easier to reverse the logic and remove the
NOT operator
– De Morgan’s Law provides two forms: one for negation of
an AND expression and one for an OR expression
De Morgan’s Law for opposite relationships
Chris Simber 2023 – OER Text Resource
63
Decision Structures and Boolean Logic
• Logical Not (!) Operator
– When a Boolean variable is involved, the NOT operator
can be used without adding confusion
– Either of these is a correct implementation of the logic
Chris Simber 2023 – OER Text Resource
64
Decision Structures and Boolean Logic
• Test Cases
– As programs become more complex, testing becomes
more complex and involved
– Test Cases are actions executed to verify a portion of and
the complete software implementation
– All possible paths through a program must be verified
– Test Cases ensure complete test coverage
Test Cases document the accuracy of the program
Chris Simber 2023 – OER Text Resource
65
Decision Structures and Boolean Logic
• Test Cases
– Improve the quality of software
– Decrease maintenance and support costs
– Verify requirements
– Are retested when a change is made to the software to
verify that it still works correctly
– Document the validation of the program
Chris Simber 2023 – OER Text Resource
66
Decision Structures and Boolean Logic
• Test Cases
– The Theater Ticket conditional expression has two
Boolean conditions that need to be tested
Chris Simber 2023 – OER Text Resource
67
Decision Structures and Boolean Logic
• Test Cases
– There is one scenario in which the expression is true
Chris Simber 2023 – OER Text Resource
68
Decision Structures and Boolean Logic
• Test Cases
– Should be simple to execute
– Have the end user of the program in mind
– Ensure 100% coverage
– Be repeatable (the same results whenever run)
– Be numbered or labeled for tracking purposes and
regression testing
Test Cases improve the quality of the software
Chris Simber 2023 – OER Text Resource
69
Decision Structures and Boolean Logic
Chapter 3
Decision Structures and Boolean Logic
Creative Commons — Attribution-NonCommercial-ShareAlike 4.0 International — CC BY-NC-SA 4.0
Under the following terms: Attribution — You must give appropriate credit, provide a link to the license, and
indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests
the licensor endorses you or your use.
creativecommons.org
Chris Simber 2023 – OER Text Resource
70