201Spring08-L14 - CMPSC 201C Spring 2008 Lecture 14...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CMPSC 201C Spring 2008 Lecture 14 February 15, 2008 Shorthand Operators Provide a shortcut for assignment statements. Combines the = operator with another arithmetic operator a + = 5.67; is equivalent to a = a + 5.67; a -= 6.0; is equivalent to a = a 6.0; a *= 3.4; is equivalent to a = a * 3.4; a /= 2.1; is equivalent to a = a /2.1; a %= 3; is equivalent to a = a % 3; Reminder---the % operator only works for integer data types. Incrementing Adding one to a variable is called incrementing that variable (usually an integer). A = A + 1; This statement can be replaced by the incrementing operator ++. A++; or ++A; (Post-incrementing) (Pre-incrementing) Decrementing Subtracting one from a variable is called decrementing that variable. A = A - 1; This statement can be replaced by the decrementing operator --. A--; or --A; (Post-decrementing) (Predecrementing) Pre vs. Post Determines when the incrementing (or decrementing occurs), before or after the value is used. Does not matter with a stand-alone statement such as a++; Effects the results when the variable is combined with other values. y = a++ * b; y = ++a * b; y = a * b; a = a + 1; a = a + 1; y = a * b; Questions ????? Formatting Output By default the insertion operator, <<, does not output spaces or control the number of digits output for a number. However, C++ has additional manipulators (see table 3.7 on page 86) that can aid in this process. Many manipulators require the inclusion of the <iomanip> header file. setw() Manipulator You can control the number of spaces that will be used to output an item by using the setw(n) manipulator. cout<<manipulator<<item; cout<<setw(10)<<x; // allows 10 spaces The n must be an integer and is often called the field width specification. By default numbers are right justified within this field. Must be before each item to be output. setprecision() Manipulator The setprecision(n) manipulator may be used to control the number of significant digits or, if used with the fixed manipulator, the number of decimal places that will be displayed for a floating point number. cout<<setprecision(3)<<y; cout<<fixed<<setprecision(3)<<y; Both fixed and setprecision() remain in effect until a new manipulator is used. Has no effect on integer values! Other Manipulators Other manipulators may be used to control how data is displayed, e.g. showpoint, left, right. The manipulator showpoint is used to show the decimal point and zeroes for numbers like 45.0 Can use more than one manipulator at a time, just separate with the << operator. The programmer can also change manipulators within the same cout statement. cout<<left<<somevariable<<right<<var2; Removing Fixed flag To remove the fixed flag so setprecision() displays a number of significant digits rather that a specified number of decimal places you may use the reset command cout<<resetiosflags(ios::fixed); This may be a stand alone statement or combined in another cout statement Questions Control Structures There are 4 basic control structures or ways flow of control of a program may be affected. Sequence one statement executed after another. Decision/Branches statements may or may not be executed depending on specified conditions. Repetition/loops statements are executed over and over. Functions control goes to another block of code designed for a specific purpose. Loops (Repetition) false Condition true Statement(s) General Format of a While Loop A while loop has the general format of while (loop condition) { statements( ) //body of loop } Sentinel vs. Count Control Sentinel control means loop may run for an indeterminate number of times. It ends when the sentinel value is obtained. Count control means that the loop runs for a predetermined number of times. The loop control variable, counter, is updated in systematic manner every time the loop is executed. Example Count Controlled int counter = 1; while ( counter < 25) { statement(s) counter++; } Loops ends when counter reaches 25. The statements are executed 24 times (counter = 1, 2, 3, .......24) Example Sentinel-Controlled cin>>radius; while (radius < 0) { cout<<"You entered an invalid number," << " please reenter the radius."<<endl; cin>>radius; } Common Errors Not reaching the termination condition - loop never ends. It is an infinite loop. Missing braces - only first statement is executed as body of the loop. A semicolon at the end of while line while (condition); No statement is executed - it is an empty loop and may be an infinite loop once it starts. Questions ???? ...
View Full Document

This note was uploaded on 03/19/2008 for the course CMPSC 201 taught by Professor Susanquick during the Spring '08 term at Pennsylvania State University, University Park.

Ask a homework question - tutors are online