CMSC 131A Final: Final Exam (2/2) - Fall 2017

237 views51 pages

Document Summary

The for each style provides an easy way to visit everything in (for example) a collection exactly once. While loops are common if you do not know in advance how many times the task needs to be performed or if the order/progression is not based on a simple notion of a next thing. A group wrote a free e-mail program that was meant to replace it. Most programming languages provide a way to have a subroutine (method in java) call itself if desired. It is also important to have a well-defined stopping point that does not use a recursive call (sometimes called a base case). It is important to make sure you are doing the correct pre and post processing around the recursive calls. Note that factorial grows somewhat fast so the return value has been made double to avoid overflow but this can sacrifice some precision. public static double factorialfor(int n) { double returnvalue = 1;