Some Common Escape Sequence Example

Some Common Escape Sequence Example

Class # 3 Topics: Summary from previous class January 7, 2001 Introduction to C++ Programming Some Common Escape Sequence (cont) An Addition Program (using cin) Memory Concepts Arithmetic Operation Precedence of Arithmetic Operators Zeki Aliyazicioglu Decision Making: Equality and relational Operators Electrical & Computer Eng. Cal Poly A simple Program: Printing a Line of Text Some Common Escape Sequence // Fig. 1.2: fig01_02.cpp // indicates that the remainder of each line is a command // A first program in C++ \n New line. Goes to beginning of the next line #include <iostream> Including in the program the contents of the input/output stream header file \t Horizontal tab Move the screen cursor to the next tab stop int main() Main is a program building block called a function \r Carriage return. Position the screen cursor to the { beginning of the current line. std::cout << "Welcome to C++!\n"; Print on the screen the string of characters \a Alert. Sound the system bell \\ Backslash. Print a backslash character return 0; // indicate that program ended successfully \” Double quote. Print double quote character } Output Welcome to c++! Example: Example: 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements 1 // Fig. 1.5: fig01_05.cpp 3 #include <iostream> 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 A Single statement can print 4 multiple lines by using new-line 5 int main() The first stream characters 6 { insertion prints Welcome 5 int main() 7 std::cout << "Welcome "; followed by space and 6 { 8 std::cout << "to C++!\n"; second stream insertion 7 std::cout << "Welcome\nto\n\nC++!\n"; begins print to C++! 9 8 10 return 0; // indicate that program ended successfully 9 return 0; // indicate that program ended successfully 11 } 10 } 1 Example: An Addition Program Fig 1-6 (cont) 1 // Fig. 1.6: fig01_06.cpp 11 std::cout << "Enter second integer\n"; // prompt 2 // Addition program 12 std::cin >> integer2; // read an integer 3 #include <iostream> 4 13 sum = integer1 + integer2; // assignment of sum 5 int main() 14 std::cout << "Sum is " << sum <<std::endl; // print 6 { sum 7 int integer1, integer2, sum; // declaration 15 8 16 return 0; // indicate that program ended successfully 9 std::cout << "Enter first integer\n"; // prompt 17 } 10 std::cin >> integer1; // read an integer Notes: (Continue) Notes: The other data types l double (used for specifying real number) Line int integer1, integer2, sum; is a declaration. l char (for specifying character data. May hold only a single integer1, integer2, and sum are the names of letter. variables. A variable name is any valid identifier, which Line 10 l series of character consisting of letter, digits, and underscore std::cin >> integer1 (_) that does not begin with a digit. l Uses the input stream object cin and the stream l Case sensitive extraction operator >> to obtain value from the l Use max 31 characters keyboard. l No reserved or keyword l It waits for user to enter a value for integere1 The declaration specifies the data types int of l User responds by typing an integer number then variables. These variables will hold integer values press the Enter key to send the number the All variables must be declared with name and data computer type before using in the program l This number will be integer1 value. Notes: (Continue) Memory Concepts Line 13 sum = integer1 + integer2; Every variable has a name, a type, a size and Calculates sum of the variables integre1 and intger2 and assign the result to variable sum using a value. assignment operator =. Each variable is placed into a memory location. Line 14 Whenever a value is placed in a memory std::cout << "Sum is " << sum << endl; location, the value replaces the previous Prints character string Sum is followed by the value in that location. The previous value is numerical value of variable sum. lost. std::endl manipulator outputs a new-line then “flushes the output buffer” 2 Arithmetic Operation Precedence of Arithmetic Operators C+++ Arithmetic Algebraic C++ Operation Operation Operations Expression Addition + f + 7 f + 7 Operators Operations Order of Evaluation Subtraction - p - c p - c Evaluate first. If there are ( ) Parentheses several, evaluate left to right Multiplication * bm b * m Multiplication Evaluate second. If there are *,/,or % Division several evaluate left to right Division / x / y x / y Modulus Addition Evaluate last. If there are Modulus % r mod s r % s + or - Subtraction several, evaluate left to right 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us