Chapter 5 PDF Flashcards
Describe sequence in the flow of control
Execute instructions in order
Describe method calls in the flow of control
Transfer control to method, execute instructions in method, then return with or without a value
Describe selection in the flow of control
Execute different instructions depending on the data
Describe looping in the flow of control
Repeat a set of instructions for different data
Describe equality operators
They’re used to determine if the values of two expressions are equal or not equal
What is special about equality operators
Must be used with a primitive data type and object references, not to compare object data
What is a common error made with equality operators
Equality operators (==) and easily confused with the assignment operator (=)
What is relational operators used for
Relational Operators are used to compare the values of two expressions
What is the result for relational operators
The result for relational operators is either true or false
What is ! used for in programming
! is the NOT operator
What is the ! (NOT OPERATOR) used for
The NOT operator is used to evaluate to the inverse of the value of its operand. (EX: If the operand is true the result will be the opposite)
What is && used for in programming
&& is the AND operator
What is the && (AND OPERATOR) used for
The AND operator takes two boolean expressions as operands, if both operands are true then the result is true. Otherwise the result is false.
What is || used for in programming
|| is the OR operator
What is the OR operator used for
The OR operator takes two boolean expressions as operands. If both operands are false, the result will be false and otherwise it’ll be true.
In any logical operator, how are the operands evaluated?
Operands are evaluated left to right for any logical operator
What occurs if the result of the logical operator can be determined after evaluating the first operand
Only the first operand will be evaluated and the second one will be ignored
What is the result if the first operand of an OR statement is true
The result of the statement will be TRUE
What is the result if the first operand of an AND statement is false
The result of the statement will be FALSE
What is a common error when testing three ints
Each operand of a logical operator must be a boolean expression or else you will have a compiler error
What are DeMorgans Laws
- NOT( A AND B ) = ( NOT A ) OR ( NOT B )
2. NOT( A OR B ) = ( NOT A ) AND ( NOT B )
How would you find an equivalent expression using DeMorgans Laws
- Change AND to OR
- Change OR to AND
- NEGATE EACH OPERAND EXPRESSION
What is an if statement used for
If statements are used when the program should perform an operation for one set or data but do nothing for all other data
What is special about curly brackets if the true block of an if statement only contains one statement
Curly braces are optional if the true block contains only one statement
Describe a simple IF Flow of Control
If the condition evaluates to true, the true block is executed.
If the condition evaluates to false, the true block is skipped
What is a a common error with if statements
Do not put a semicolon after the condition in an if statement. Doing so would indicate that the true block is empty and will cause a logic error
When is the if/else statement used
Used when Data falls into two mutually exclusive categories and the program should perform different operations for each set of data
What is special about curly brackets if the true block of an if/else statement only contains one statement
Curly braces are optional if the true block contains only one statement
Describe an id/else flow of control
If the condition is true, then the true block is executed.
If the condition is false then the false block is executed
What is scope
The region within a program where an identifier can be referenced.
Where does the scope of a variable end and begin
The scope of a variable extends from the point of declaration to the end of the block in which it is declared
What are some examples of blocks
Classes
Methods
If statements true and false blocks
When is an if/else if statement used
Used when data falls into multiple, mutually exclusive categories and the program should perform different operations for each set of data
Describe the if/else if Flow of Control
- If the first condition is true, the first true block is executed. The rest of the if/else statement is skipped.
- If the n^th condition is true then the n^th true block is executed. The rest of the if/else statement is skipped.
- If no condition is true, the false block is executed.
What are nested if statements
If statements can be written as part of the true or false block of another if statement.
What is needed if you nest an if statement
If you nest an IF statement more information is required beyond the results of the first if condition
What occurs if you don’t assign an else clause in a nested if statement
The compiler will match any else clause with the most previous if statements that doesn’t already have an else clause
What can curly brackets be used for in a nested if/else statements
You can use curly brackets to force a desired if/else pairing
What is a “Dangling Else”
A dangling else occurs when an else clause cannot be paid with an if condition
What can occur with floating point representation
Minor rounding errors can occur in calculations, causing the equality operator not to consider the values equal.
How do we solve the equality operator not being able to consider values equal (in relation to rounding errors)
Consider a small threshold value. If the absolute value of the difference between the two values is less than the threshold value, then the two floating point numbers will be considered equal.
What does the equality operator compare
The equality operator compares object references. ie: compares that they point to the same object… doesn’t compare the data
How would you compare object data instead of comparing the objects themselves
You would use the equals method
What is the common error when using equality operators
You do not use equality operators to compare object data, instead use the equals method
What are strings considered
Strings are objects (data)
How would you compare two strings
Use the equals method since Strings are objects (data)
What does a conditional operator do
A conditional operator contributes one of two values to an expression based on the value of the condition
What are some uses of conditional operators
Handling invalid input
Outputting similar messages
When is a switch statement used
In some selection operators, the switch statement can be used instead of an if/else/if statement
What are the requirements for using a switch statement
Can be used when we are comparing the value of an expression to constant intergers (byte, short, int), characters (char) or Strings.
How is the switch operation performed
The expression is evaluated, then its value is compared to the case constants in order. When a match is found, the statements following that case constant are executed in sequence until either a break statement or the end of a switch block is reached.
What are some optional aspects when using a switch statement
The break statement is optional
The default label and its statements are also optional
The statements under the case constant are also optional
Why is the break statement optional when using a switch statement
The break statement is optional when using a switch statement because the job of the break is to terminate execution of the switch statement
Why is the default label and its statements optional when using the switch statement
The default label is optional because they are executed only when the value of the expression does not match any of the case constants
Why are the statements under the case constant optional when using a switch statement
The statements under the case constant are also optional so multiple case constants can be written in sequence if identical operations will be performed for those values
What are common errors with break statements
(Remember that once a match is found, all succeeding statements are executed until a break statement is encountered).
Be careful to code a break statement when you have finished the processing of a value, otherwise you may execute unintended statements