1 / 19

Week 3

Week 3. In this class, you will learn about: Program Structure, Actions and Data Types Data Type: Variables Output and Input action: function cout<< & cin>> Assignment Simple calculations

autumn-tate
Télécharger la présentation

Week 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 3 In this class, you will learn about: • Program Structure, Actions and Data Types • Data Type: Variables • Output and Input action: function cout<< &cin>> • Assignment • Simple calculations Reference: Programming with C++, Theory and Problems of, Second Edition; John R Hubbard (McGraw Hill, 2000, Schaum's Outline Series)

  2. Program Structure, Actions and Data Types • C++ Program Structure • #include <iostream> • main() { • cout<<"HEY, you, I'm alive!"; // output a string. • } • The program is saved as a file that should be named. In this example, the name of the file is cout1a.cpp. .cpp • The #include is called a preprocessor directive which tells the computer compiler to include the header fileiostream.h in our program cout1a.cpp. • The next part is main(). • The body of the • The lines for the body of a program is called statements, for example, cout • Comment lines

  3. Data Type: Variables • Variables • Name of a variable • A variable has a name, which is given by the programmer by declaration or definition. When the name is given, the memory for it is specified. • Rules for creating names of variables • Rule 1: a name starts with a letter. • Rule 2: a name is unique, which should be different from other words in the program. • Rule 3: names are sensitive to letters upper case or lower case. Doug is not equivalent to doug. • Rule 4: names should be meaningful.

  4. For example, int alice; name of variable: alice type: int(eger) tail: ; Data Type: Variables

  5. Data Types

  6. Part of Integral Type

  7. It shows that the 32 bits it uses to store a float are partitioned into 3 parts: 23 bits for the mantissa, 8 bits for the exponent, and 1 bit for the sign. The 23-bit mantissa produces a floating-point value with 6 significant digits, and the 8-bit exponent yields a range in magnitude from about 10–37 to about 3 × 1038. i.e., 0.0000000000000000000000000000000000001 < |x| < 300,000,000,000,000,000,000,000,000,000,000,000,000 for any variable x declared to have type float. Float type example

  8. A new variable : char (character data) • Definition • A char variable is defined to hold a single character that appears on the keyboard. • Difference between a char and a string is that a char is a single character while a string is a collection of serial characters. • Declaration • char c; • Input a char variable from keyboard • A space cannot be input from keyboard

  9. A new variable : char (character data)

  10. A new variable : char (character data)

  11. Output action: function cout<< • More actions by using a back slash \ • The sign \ is not a normal character but a control character to instruct the computer to take an action corresponding to next character. See table below, • Escape characterEffect\nNewline\tHorizontal tab\rCarriage return\bBackspace\aBell or beep\\Backslash\”Double quote\’Single quote\?Question mark

  12. Input action: input function cin>>Assignment • We have seen that one way of assigning a variable with a value is to input a number of digits on keyboard. Another important way is to use an assignment statement. For example (assignment1.cpp),

  13. Prefix and postfix increment • Example:k = ++x;//prefix increment • is equivalent to: • x = x + 1; //increment n first • k = x;//assign x’s value to k • Example:k = x++;//postfix increment • is equivalent to • k = x;//assign x’s value to k • x = x + 1;//and then increment x

  14. Applying the Pre-increment and Post-increment Operators int main() { // shows the difference between m++ and ++m: int m,n; m = 44; n = ++m; // the pre-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; m = 44; n = m++; // the post-increment operator is applied to m cout << "m = " << m << ", n = " << n << endl; }

  15. COMPOSITE ASSIGNMENT OPERATORS • The standard assignment operator in C++ is the equals sign =. In addition to this operator, C++ also includes the following composite assignment operators: +=, -=, *=, /=, and %=. • When applied to a variable on the left, each applies the indicated arithmetic operation to it using the value of the expression on the right.

  16. Applying Composite Arithmetic Assignment Operators int main() { // tests arithmetic assignment operators: int n=22; cout << "n = " << n << endl; n += 9; // adds 9 to n cout << "After n += 9, n = " << n << endl; n -= 5; // subtracts 5 from n cout << "After n -= 5, n = " << n << endl; n *= 2; // multiplies n by 3 cout << "After n *= 2, n = " << n << endl; n / = 3; // divides n by 9 cout << "After n /= 3, n = " << n << endl; n %= 7; // reduces n to the remainder from dividing by 4 cout << "After n %= 7, n = " << n << endl; }

  17. Simple calculations

  18. Simple calculations

  19. Integer Calculation Example int main() { // tests operators +,-,*,/,and %: int m=54; int n=20; cout << "m = " << m << " and n = " << n << endl; cout << "m+n = " << m+n << endl; // 54+20 = 74 cout << "m-n = " << m-n << endl; // 54-20 = 34 cout << "m*n = " << m*n << endl; // 54*20 = 1080 cout << "m/n = " << m/n << endl; // 54/20 = 2 cout << "m%n = " << m%n << endl; // 54%20 = 14 Return 0; }

More Related