1/6
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
The Loop Body
• Write down a sequence of actions when designing a loop.
Initializing Statements
• A loop depends on correct initial values.
Controlling the Number of Loop Iterations
• A counter can control a loop's repetition.
• A loop's repetition can be controlled by asking the user whether to continue.
• User can signal the end of repetition by entering a sentinel value.
The break Statement and continue Statement in Loops
• A break statement within a loop ends its iteration.
• A continue statement within a loop ends the current iteration.
• As much as possible, avoid the break and continue Statement in Loops.
Loop Bugs
• Two common loop bugs
- Unintended infinite loops.
- Off-by-one errors.
• Certain data can cause an infinite loop.
• Code should try to guard against user error.
• Off-by-one errors are caused by an incorrect boolean expression.
• Do not compare floating-point values using == or !=.
• Off-by-one errors might be unnoticed.
• Always retest.
Tracing Variables
• Trace variables as their values changes during execution.
• add //* at the end, to keep track of where a trace variable is.
Assertion Checks
• An assertion states a truth if a program's logic is correct.
• An assert statement checks an assertion.
• Assertion checking can be on or off.
• Assert Boolean_Expression;
• You can place an assertion check anywhere in your code. If assertion checking is turned on and Boolean_Expression is false, your program will display a suitable error message and end execution. If assertion checking is not turned on, the assertion check is treated as a comment.