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

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Chapter 2: Basic Elements of C++
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
CSC 107 – Programming For Science. Announcements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Operating System Using setw and setprecision functions Using setiosflags function Using cin function Programming 1 DCT
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Chapter 2: Basic Elements of C++
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 2: Basic Elements of C++. Objectives In this chapter, you will: – Become familiar with the basic components of a C++ program, including functions,
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Arithmetic Expressions Function Calls Output
Computer Programming BCT 1113
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 2: Basic Elements of C++
Introduction to C++ Programming
Chapter 3: Input/Output
Chapter 3 Input output.
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
elementary programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
C++ Programming Basics
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

arithmetic operator & cin I.Mona Alshehri

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

Example: cout<<" "<<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: ALA

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

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)* /(32-1) + 8%3

Example Arithmetic ExpressionResultDescription * / / 7 2

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

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;

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--

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 }

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

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

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

: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;

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)

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

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

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

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

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