C Chapter 2 Beginning to Program in C

C++程式設計 哈爾濱 業大學 電腦科學與技術學院 Chapter 2 Beginning to Program in C++ sxh@hit. edu. cn 1

2. 1 Constants(常量) u Data in C++:Constants;variables u Constants:does Constants not change its value in a program. 整型:int 浮點型:float 字元型:char 字串型:String 哈爾濱 業大學 電腦科學與技術學院 2

2. 2 Variables(變數) u Variables:can l vary its values in a program a variable must be defined before it can be used. A variable is defined by giving it a data type and a name main( ) marks the point where a C++ program starts to execute and mustappear once defines variables’ name and type Assign value to variables • The program statements are contained within the braces { and } 哈爾濱 業大學 電腦科學與技術學院 • Each statement ends with ’; ’ 3

2. 2 Variables u. A variable can be given any name, called anidentifier in C++, provided it is within the following rules l An identifier can only be constructed usingletters, numeralsorunderscores (_). l An identifier muststart with a letter or an underscore. l An identifier cannot be a C++keyword. A keyword is a word that has a special meaning. l An identifier can contain any number of characters, but only the first thirty-one characters are significant to the C++ compiler. u. A variable is like a box in the memory of the computer. 哈爾濱 業大學 電腦科學與技術學院 4

2. 3 Simple output to the screen #include <iostream. h> l cout directive causes the file to make available to the program; <iostream. h> contains some • a string of characters: letters or numbers enclosed in“ ” • The variable name is not in quotation marks C++ statements to make it easy to perform input and output • cout: sends the data to cout • the stream insertion operator<< is used to insert a string of characters into the output stream object cout • endl: go the start of the next line on the screen 哈爾濱 業大學 電腦科學與技術學院 5

2. 4 Comments(注釋) u Comments are added to a C++ program to make it more readable for the programmer l completely ignored by the compiler. l comments start with the characters//and end at the end of the line. 哈爾濱 業大學 電腦科學與技術學院 6

2. 4 Comments u C++ comments cannot span more than one line. l Typically, comments are placed at the start of the program to describe the purpose of the program, the author, date written and any other relevant information l C-style comments begin with the characters /* and end with the characters */ is also used in C++ l Get into the habit of using comments l bad comments can lead to confusion 哈爾濱 業大學 電腦科學與技術學院 7

2. 5 Data types(資料類型) u Different data types require different amounts of memory and therefore vary in the range of values they can store. l short integer:短整型 l Long Integer data types: 長整型 l Boolean data types:布林資料類型 ü can store only one of two values: true(1) orfalse(0) l Double Floating-point data types: increase the range and precision(or accuracy) of a floating-point number. 哈爾濱 業大學 電腦科學與技術學院 8

2. 6 Data type sizes u sizeof operator to display the number of bytes of memory required by some common data types. running results 哈爾濱 業大學 電腦科學與技術學院 9

2. 7 Operators(運算子) u 2. 7. 1 l The assignment operator(設定運算子)“=” assign values to variables. u 2. 7. 2 Arithmetic operators(代數運算子) %: 取餘運算子, 獲取 除法運算的餘數 哈爾濱 業大學 電腦科學與技術學院 10

2. 7 Operators u Program Example P 2 E running results 哈爾濱 業大學 電腦科學與技術學院 11

2. 7 Operators u 2. 7. 3 Increment(自加)、 decrement(自減) operators l ++: adds 1 to the value of a variable l --:subtracts 1 from the variable var 1 l The increment operator ++ has two forms, prefix and postfix 哈爾濱 業大學 電腦科學與技術學院 12

2. 7 Operators u Program Example P 2 F • Line 9 of this program defines the variables var 1 and var 2 as integers and initialises them to 1 and 2, respectively. Line 15 adds 1 to var 1, and line 16 subtracts 1 from var 2. • Lines 19 and 20 display the final value of var 1 and var 2. • 哈爾濱 業大學 電腦科學與技術學院 13

2. 7 Operators u Program Example P 2 G 運行結果 • prefix----If the ++ is before a variable, the variable is incremented before it is used. • postfix ----If the ++ is after the variable, the variable is used and then incremented. • The decrement operator -also has prefix and postfix forms. 哈爾濱 業大學 電腦科學與技術學院 14

2. 7 Operators u 2. 7. 4 Combined assignment operators l The += operator adds the value on its right to the variable on its left. l There are five combined assignment operators: +=, -=, *=, /=, and %=, corresponding to the five arithmetic operators +, -, *, /, and %. 哈爾濱 業大學 電腦科學與技術學院 15

2. 8 Operator precedence(運算子的優先順序) u Consider the following statement: l 2 + 7* 8 = 72 var × l 2 + 7* 8 = 58 var √ u Operator precedence 1)*,/have a higher priority than+、2)Expressions containing operators of the same precedence are evaluated according to theirassociativity 哈爾濱 業大學 電腦科學與技術學院 3)use()to change the order of evaluation 16

2. 8 Operator precedence u Using parentheses will remove any confusion about which operations are done first. u unary minus: l “-” appears before an operand l highest priority u binary minus:appears between two operands 哈爾濱 業大學 電腦科學與技術學院 17

2. 9 Type conversions and casts(類型轉換和強轉) u Automatic conversion-----Program Example P 2 H For calculations involving mixed data types, C++ automatically converts the value. running results 哈爾濱 業大學 電腦科學與技術學院 18

2. 9 Type conversions and casts u When doing calculations involving mixed data types, C++ ranks the data types in this order: u promotion orwidening of the data l For calculations involving mixed data types, C++ automatically converts the value in the lower data type to a higher type. l Promotion will cause no loss of data, because the higher data types occupy more memory than the lower types and can therefore hold the data precisely. 哈爾濱 業大學 電腦科學與技術學院 19

2. 9 Type conversions and casts u demotionornarrowing of the data l when data is assigned to a variable of a lower type l Demotion may result in a loss of data, because the lower data type may not have enough bytes of memory to store the higher data type. 哈爾濱 業大學 電腦科學與技術學院 20

2. 9 Type conversions and casts u Manual l conversion with astatic cast Using a static cast is equivalent to assigning the expression to a variable of the specified type and using that variable in place of the expression. The expression var 1/4. 0 is mixed, and the value of var 1 is therefore promoted to a floating point value automatically. 哈爾濱 業大學 電腦科學與技術學院 21

Programming pitfalls u 1. Do not type a semicolon after either u 2. End each C++ statement with a semicolon. u 3. A semicolon is not always at the end of a line. 哈爾濱 業大學 電腦科學與技術學院 This will cause a compiler error, because the semicolon is part of the comment. 22

Programming pitfalls u 4. A typing error may result in a statement that does nothing but that is valid nonetheless. 哈爾濱 業大學 電腦科學與技術學院 23

Programming pitfalls u 5. You must initialise a variable before using it in an arithmetic expression. u 6. Be aware of the operator precedence rules. ü If you are unsure, use parentheses. ü The number of opening and closing parentheses should be equal. u 7. Each variable has an associated data type (int, float, etc. ). Be careful not to go outside the range of valid values for a variable. 哈爾濱 業大學 電腦科學與技術學院 24

Programming pitfalls u 8. You may not always get what you expect when doing arithmetic in C++. l variable f will contain 12, and not the 12. 5 l Use a static cast if the fractional part of the result is required. 哈爾濱 業大學 電腦科學與技術學院 25

Programming pitfalls u 9. Avoid unnecessary comments in your program. 哈爾濱 業大學 電腦科學與技術學院 26



- Slides: 29