While Loop Warnings The Loop Control Variable LCV
While Loop Warnings The Loop Control Variable (LCV) must be initialized properly to guarantee that the loop body executes one or more times. The LCV must change inside the loop body to exit after the intended number of repetitions.
Algorithm Definition An algorithm is an unambiguous, step-by-step solution to a problem.
Algorithm Example Find the average (mean) of 5 numbers. 1. Enter 5 numbers. 2. Add them and store the total. 3. Divide the total by 5. The result is the mean. 4. Display the mean.
Euclid’s GCF Algorithm Step 1 Find the remainder of dividing number 1 by number 2. Step 2 If the remainder equals 0 You are finished and the GCF is number 2 Step 3 If the remainder is not 0, then number 1 becomes number 2 and number 2 becomes the remainder. Step 4 Repeat the process by returning to step 1.
Euclid’s GCF Algorithm Example Let us use this algorithm to find the GCF of 120 &108. Step 1: 120 / 108 = 6 with remainder of 12 Step 2: 12 ≠ 0 Step 3: The 2 numbers are now 108 and 12. Step 4: Return to Step 1: 108 / 12 = 9 with remainder 0 Step 2: The remainder equals 0. The GCF is 12.
Fact: Hard-coding Variable Values is Not Practical.
Fact: The Mathematical Foundation of Computer Programming is Boolean Algebra.
® AP CS(A) Exam Alert Program Input/Output Processing data with external files, called program input/output, is an important topic in Computer Science. It is an excellent topic for Post-AP-Exam activities, but program i/o will not be tested on the AP Exam.
DO NOT DO THIS! Even though it compiles and executes, it is also very hard to read. Make sure you use a readable, consistent format.
Make sure that your programs execute properly for all values. Check for Logic Errors.
This program works better but it does not protect against Erroneous Input.
This program protects against one kind of Erroneous Input, but not another.
- Slides: 79