REED MATH 121 - Essential Idioms

Unformatted text preview:

Math 121: Introduction to Computing Handout #5Essential IdiomsYour programming instincts will be helped by learning the idioms and patterns thatimplement particular problem-solving strategies. These idioms give you a considerableamount of power without requiring you to learn too many of their component details.This sheet summarizes the most important idioms, each of which is covered in moredetail in the text.The first set of idioms involves getting data in and out of the computer, which provide thenecessary support for the input and output phases of a typical programming task. Theidioms you use depend on the type of value, as shown in the following table:Type Declaration Input idiomIntegerint var = value; var = readInt("prompt");Floating-pointdouble var = value; var = readDouble("prompt");StringString var = value; var = readLine("prompt");The following idioms are useful in calculations:English Java (long form) Java (shorthand form)Add y to x.x = x + y; x += y;Subtract y from x.x = x - y; x -= y;Add 1 to x (increment x).x = x + 1; x++;Subtract 1 from x (decrement x).x = x - 1; x--;The most helpful idioms, however, encompass programming operations on a larger scaleand help you establish the overall strategy of a program. The most important ones aredescribed in the next few sections.The repeat-N-times idiom:This idiom is the same as it was in Karel and is used for the same purpose.for (int i = 0; i < N; i++) { statements to be repeated}– 2 –In Java, however, you are allowed to use the value of the index variable i in the body ofthe loop.The repeat-until-sentinel idiom: This idiom is useful whenever you need to read in values until the user enters a particularvalue to signal the end of input. Such values are called sentinels.while (true) { prompt user and read in a value if (value == sentinel) break; rest of


View Full Document

REED MATH 121 - Essential Idioms

Download Essential Idioms
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Essential Idioms and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Essential Idioms 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?