FLVS AP Computer Science A - Module 3: Selection and Repetition
AP Computer Science A - Selection and Repetition (Based on Guided Notes)
Condition Statements: IF
Objectives
Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.
Develop code to represent branching logical processes by using selection statements and determine the result of these processes.
Key Questions and Terms
Define branching.
Flow of control that allows different and separate pieces of code to be executed based on a condition.
What are binary decisions?
Decisions between two choices e.g. yes/no, true/false, on/off.
What happens if you put a semicolon at the end of an IF statement?
The program won’t run as intended as a semicolon indicates the end of an if block and tells the program to move on.
What is a block of code?
A piece of code between a pair of opening and closing curly braces.
Condition Statements: IF-ELSE
Objectives
Develop code to represent branching logical processes by using selection statements and determine the result of these processes.
Condition Statements: IF-ELSE-IF
Objectives
Develop code to represent branching logical processes by using selection statements and determine the result of these processes.
Comparing Strings
Objectives
Develop code to call methods on string objects and determine the result of calling these methods.
Key Questions and Terms
Explain the difference between an IF and an IF-ELSE statement.
An if statement only cares about whether the condition inside is true, and moves on if it’s false. An if-else statement checks for the first condition, and then checks for another condition if the first condition is false.
Explain the difference between an IF-ELSE and an IF-ELSE-IF statement.
An IF-ELSE statement checks for one condition, while an IF-ELSE-IF checks for at least two conditions.
Where can problems arise in IF-ELSE statements?
Problems can arise if we don’t know how to check for multiple conditions at once; we can’t get it to work the way we need it to.
Describe how nested IF statements work.
Nested IF statements consist of multiple if statements, usually in the form of embedding at least one IF/IF-ELSE/IF-ELSE-IF statement inside another IF-ELSE/IF/IF-ELSE-IF statement.
Define identity equality.
Whether two objects are the exact same object.
What is content equality?
Whether two objects contain data/info that matches one another.
Logical Operators
Objectives
Develop code to represent compound Boolean expressions and determine the result of these expressions.
Key Questions and Terms
What is the role of the equals() method?
Checks for the content equality of a string object with a specified string; it compares the value/contents of a string to see whether it’s identical to an existing string.
Describe how each of the String methods works:
toUpperCase(): Converts all characters in the String to uppercase.
compareTo(): Compares two strings to see which one would go first alphabetically. A negative integer is returned if the first string is ordered alphabetically before the second, and a positive integer if the second comes first. A result of 0 indicates that the contents of both strings are exactly the same.
equalsIgnoreCase(): Compares the contents of a string to another string, ignoring capitalization; returns true if the spelling matches regardless.
toLowerCase(): Converts all characters in the string to lowercase.
compareToIgnoreCase(): Same as compareTo(), but ignores capitalization.
What does it mean when words are sorted lexicographically?
It means that they are sorted alphabetically as if they were in a dictionary.
While Loops
Objectives
Develop code to represent iterative processes using while loops and determine the result of these processes.
Key Questions and Terms
What is a logical operator?
An operator that can be applied to Boolean values or expressions.
How does the && operator work?
"And" operator; it evaluates both Boolean conditions and executes the code within the block if both are true.
Explain how the || operator works.
"Or" operator; it evaluates both conditions. If at least one of them is true, it executes the code within the block.
How does the ! operator work?
It negates the outcome of a Boolean expression; it checks to see whether a certain outcome has NOT been achieved and returns true if so.
What is meant by negation?
Using the logical operator ! (Not) to reverse the evaluation of a Boolean expression and ensure that a specified condition doesn’t happen before returning true.
How can you modify the statement !(a || b) to remove the NOT operator?
The modified statement would be (a == false || b == false).
How do truth tables work?
Truth tables list every possible outcome based on every possible combination of inputs, usually for compound Boolean expressions.
What is meant by short-circuit evaluation?
Short-circuit evaluation occurs when only one part of an expression is evaluated to decide whether the entire expression should output true or false.
Explain De Morgan’s Laws:
First Law: !(A && B) is the same as !A || !B; if both A & B are false, you will get the same output as having either A or B be false.
Second Law: !(A || B) is the same as !A && !B; if either A or B are false, you will get the same output as having both A & B be false.
Iterative Processes with While Loops
Objectives
Identify when an iterative process is required to achieve a desired result.
Develop code to write expressions that incorporate calls to built-in mathematical libraries and determine the value that is produced as a result.
Key Questions and Terms
What is repetition?
A way to repeat one or more statements.
What is a loop?
A loop is a repetitive cycle that starts over again once it finishes one iteration (until a terminating condition is met).
How does a while loop work?
If a specified condition hasn’t been met, it executes the block of code inside, then checks the condition again. Only if the condition has been met will the loop stop.
What is an infinite loop?
A loop that never stops due to terminating conditions not being met.
How can user input be used within while loops?
User input can determine whether to continue the loop for one more iteration or terminate it.
What are off-by-one errors?
When a program gives you a number that’s exactly 1 value larger or smaller than what you expected.
More Loops
Objectives
Develop code to represent iterative processes using for loops and determine the result of these processes.
Develop code to represent nested iterative processes and determine the result of these processes.
Calculate statement execution counts and informal run-time comparison of iterative statements.
Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms.
Develop code for standard and original algorithms that involve strings and determine the result of these algorithms.
List some of the reasons an IOException might be triggered.
A file isn’t found (e.g. you specified the wrong address or deleted it).
Permission issues (whoever created the original file isn’t allowing others to make changes to it).
Input/output errors on the device storing the file.
List some of the methods belonging to the Scanner class that are used to read text files.
next(): Takes and returns the next token in the file.
hasNext(): Checks whether there’s another token that follows; returns true if so.
hasNextLine(): Checks whether there’s another line of input for this scanner; returns true if so.
nextLine(): Advances the scanner past the current line and returns the skipped input.
Key Questions and Terms
What is the purpose of a for loop?
To count the number of iterations by ascending or decrementing a variable until the loop is finished.
Describe the logic sequence in nested loops.
The innermost loop is carried out first before moving to the loop that directly surrounds it. After the innermost loop finishes, the program moves on to the second innermost loop, and the cycle continues.
Writing Text Files
Objectives
Develop code to write data to a text file.
Key Questions and Terms
What iterative structures are used to write to text files?
The for and while loops.
What method is important to include to finish writing to a text file?
close().
Which datatype is used to cast a number to a Unicode character?
char.