The variable name can not consist more than 31 characters C is case sensitive

# The variable name can not consist more than 31

This preview shows page 23 - 35 out of 56 pages.

The variable name can not consist more than 31 characters. C++ is case sensitive, for example the following three variable represent three separate variables Sum, sum, SUM Rules of Declaring Variable (Cont.) Shortcut Arithmetic Operator A +=B; is same as A=A + B; A -=B; is same as A=A - B; A *=B; is same as A=A * B; A /=B; is same as A=A / B; A =B; is same as A=A B; Rules of Declaring Variable (Cont.) In using the short cut operators it is important to note that the variable to the left of the assignments operator is applied to the computed expression on the right. For example the expression A*=B+1 is same as A=A * (B+1). In general, the shortcut operators are faster than the conventional arithmetic operators. cout Statement The general form of C++ statement is as follow cout << expression1 << expression2 << endl; The object named cout represents the computer screen and expression1 and expression2 may take the form of object names such as GPA and first name or constants. For EXAMPLE: cout << 96 << “first_ name” <<first_name << endl; cout Statement (Cont.) The expressions are output in the same order as they are encountered in the statement - in a left to right order. When the expression end1 is encountered, a new line is generated, so any subsequent output starts at the beginning of a new line. cout Example #include < iostream> using namespace std; int main( ) { float num1 = 10.0; float num2 = 3.0; cout << ”num1 + num2 = ” << (num1 + num2) << endl << ”num1 - num2 = ” << (num1 - num2) << endl << ”num1/num2 = ”<< (num1/num2) << endl; return 0; } cout Example (Cont.) The following is the output of the above program num1 + num2 = 13 num1 - num2 = 7 num1 / num2 = 3.333333 Field width manipulator Are used to point formatted output using cout object. The setw manipulator tells cout to display the number in a specified field width. Formatting output Example // Example of using field manipulator #include<iostream> #include<iomanip> // preprocessor command that // includes manipulators using namespace std; int main( ) { cout << setw(3) << 7 << endl << setw(3) << 12 << endl << setw(3) << 105 << endl << ”---\n” << (7 + 12 + 10 - 5) << endl; return 0; } Formatting output Example (Cont.) The following is the output of the above program: 7 12 105 - - - 24 Formatting output cout Stream (Cont.) Formatted floating-point numbers require the use of three field width manipulators as follows: setw( ) // sets the total width of the display, // including the decimal point setiosflags(ios::fixed) // sets the output type // (exponential or conventional // decimal display) setprecisions ( ) // determine how many digits // can be printed to the right of // decimal point. Formatting output cout Stream (Cont.) For Example: The following code: cout << '|' << setw(12) << setiosflags (ios::fixed) << setprecision (4) << 55.57 << ’|’; will produce |_ _ _ _ _55.5700| Example: The following code cout << ’|’ << setw(12) << setiosflags (ios::left) << 150<< ’|’; Will produce the following left-justified display: |150 _ _ _ _ _ _ _ _ _| cin Stream cin and cout are defined in iostream.h preprocessor directive.  #### You've reached the end of your free preview.

Want to read all 56 pages?

• Fall '18
• Heidar Malki
• endl
• • • 