Presentation is loading. Please wait.

Presentation is loading. Please wait.

arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.

Similar presentations


Presentation on theme: "arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns."— Presentation transcript:

1

2 arithmetic operator & cin I.Mona Alshehri

3 The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns. The output is right justified. If the number of specified columns is less than the number of columns required by the output, then the output is automatically expanded to the required number of columns. To use the manipulator setw, the program must include the header file iomanip.h

4 Example: cout<<"12345678901234567890"<<endl; cout<<setw(5)<<11<<endl; cout<<setw(5)<<“ALA”<<endl<<endl; cout<<setw(6)<<123<<endl; cout<<setw(2)<<345<<setw(4)<<19<<endl; Output: 12345678901234567890 11 ALA 123 345 19

5 Arithmetic operators: Examples: f+7p-cb*m x/y r%s + addition - subtraction * multiplication / division % remainder (mod operator)

6 Rules of arithmetic operator precedence: ( ) inside to outside * /%left to right + -left to right The operators +, -, *, and / can be used with both integral and floating point data types, while % is used only for integral data type to find the remainder in ordinary division. Example: (3+4)* 5 + 2 /(32-1) + 8%3

7 Example Arithmetic ExpressionResultDescription 2 + 5 7 13 + 89 102 34 - 20 14 45 - 90 -45 2 * 7 14 5/2 2 14 / 7 2

8 Example Expression ResultDescription 34 % 5 4 In the division 34/5, the quotient is 6 and the remainder is 4. -34 % 5 -4 In the division -34 / 5, the quotient is -6 and the remainder is -4. 4 % 6 4 In the division 4/6, the quotient is 0 and the remainder is 4.

9 Assignment operator: = (right to left) example: a = 32; a = a-2; Additional assignment operators: += -= *= /= %= a=a+2; a+=2; a=a-2; a-=2; c=c*2; c*=2; c=c/2; c/=2; c=c%2; c%=2;

10 Increment operator - increment the value of a variable by 1 Decrement operator- decrement the value of a variable by 1. Pre Increment: ++variable Post Increment: variable++ Pre Decrement: --variable Post Decrement: variable--

11 Example: // Preincrementing and postincrementing #include int main() { int c; c = 5; cout << c << endl; // print 5 cout << c++ << endl; // print 5 then postincrement cout << c << endl << endl; // print 6 }

12 Example: { int c = 5; cout << c << endl; // print 5 cout << ++c << endl; // preincrement then print 6 cout << c << endl; // print 6 getch(); // successful termination }

13 A Look at cin Int num1; cout << “Enter a number: “; cin >> num1; cout cin > insertion extraction “put to” “get from” whitespace characters ignored

14 cin & cout main() { cout << cin >> }

15 :OUTPUT Statement The output statement is used to display items on the screen. The syntax of cout together with << is cout<<item; OR cout<<item_1<<item_2<<…<<item_n; OR cout<<item_1 <<item_2 <<... <<item_n;

16 Types of items 1. Numbers: integer or float numbers cout<<10; 10 cout<<1.25; 1.25 cout<<1<<4<<2; 142 cout<<-534; -534 cout<<1,25; Incorrect statement(syntax error)

17 Types of items 2. Characters: A single character must be enclosed in single quotes such as: cout<<‘A’; A cout<<‘H’<<‘e’<<‘l’<<‘l’<<‘o’; Hello cout<<‘A’<<‘ ’<<‘B’<<‘ ’<<‘C’; A B C

18 Types of items 3. Text: Multiple characters must be enclosed in double quotes such as: cout<<“Hello”; Hello cout<<“Mona Ali Mohammad”; Mona Ali Mohammad cout<<“Mona”<<“Ali”<<“Mohammad”; MonaAliMohammad cout<<“Mona”<<“ Ali ”<<“ Mohammad”; Mona Ali Mohammad

19 Types of items 4. Special Characters: The new line character : '\n‘ When \n is encountered in the string, the cursor is positioned at the beginning of the next line. \n may appear anywhere in the string. cout<<“Hello\nThere”; Hello There The tab character : '\t‘ Tab to next tab stop approx 8 spaces for each \t cout<<“Hello\tThere”; Hello There

20 Common Programming Errors 3 not initializing variables before use >> 3 forgetting >> to separate variables in cin ++-- 3 applying ++ or -- to an expression (x-y)++

21 Exercise: Write a program display your name in point that was specified by user (x,y)?


Download ppt "arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns."

Similar presentations


Ads by Google