Summary of the Primitive Types Type Domain Common operations byte 8-bit integers in the range-128 to 127 The arithmetic operators: + add multiply subtract / divide short 16-bit integers in the range-32768 to 32767 号 remainder 32-bit integers in the range The relational operators: int -2146483648to2146483647 =equal to !not equal 64-bit integers in the range less than <less or equal long -9223372036754775808to9223372036754775807 greater than >greater or equal 32-bit floating-point numbers in the range float ±1.4×1045to±3.4028235×10-38 The arithmetic operators except s 64-bit floating-point numbers in the range The relational operators double ±4.39×10322to±1.7976931348623157×10308 char 16-bit characters encoded using Unicode The relational operators The logical operators: boolean the values true and false ss add II or !not
Summary of the Primitive Types Summary of the Primitive Types Type short int long float double char boolean 8-bit integers in the range –128 to 127 16-bit integers in the range –32768 to 32767 32-bit integers in the range –2146483648 to 2146483647 64-bit integers in the range –9223372036754775808 to 9223372036754775807 32-bit floating-point numbers in the range ±1.4 x 10-45 to ±3.4028235 x 10-38 64-bit floating-point numbers in the range ±4.39 x 10-322 to ±1.7976931348623157 x 10308 16-bit characters encoded using Unicode the values true and false The arithmetic operators: + - * / % add subtract remainder divide multiply == < != <= >= equal to less than greater or equal less or equal not equal > greater than The arithmetic operators except % The relational operators: The relational operators The relational operators The logical operators: && add || or ! not Domain Common operations byte
上海交通大学交大密西根 联合学院·一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Constants and variables The simplest terms that appear in expressions are constants and variables.The value of a constant does not change during the course of a program.A variable is a placeholder for a value that can be updated as the program runs. intSum 42 (contains an int) A variable in C++is most easily envisioned as a box capable of storing a value. Each variable has the following attributes: A name,which enables you to differentiate one variable from another. A type,which specifies what type of value the variable can contain. A value,which represents the current contents of the variable. The name and type of a variable are fixed.The value changes whenever you assign a new value to the variable
Constants and Variables Constants and Variables • The simplest terms that appear in expressions are constants and variables. The value of a constant does not change during the course of a program. A variable is a placeholder for a value that can be updated as the program runs. • A variable in C++ is most easily envisioned as a box capable of storing a value. • Each variable has the following attributes: – A name, which enables you to differentiate one variable from another. – A type, which specifies what type of value the variable can contain. – A value, which represents the current contents of the variable. • The name and type of a variable are fixed. The value changes whenever you assign a new value to the variable intSum (contains an int ) 42
上海交通大学交大密西根 联合学院一 81T UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University C++Identifiers Names for variables (and other things)are called identifiers. Identifiers in C++conform to the following rules: A variable name must begin with a letter or the underscore character. The name must not be one of C++'s reserved words: asm else signed union bool enum long switch break false sizeof template auto friend new this case using unsigned throw catch float operator throws char for private typedef class goto protected true const if public try continue inline return void default 1og short volatile do mutable static while double int struct Identifiers should make their purpose obvious to the reader. Identifiers should adhere to standard conventions.Variable names,for example,should begin with a lowercase letter
C++ Identifiers C++ Identifiers • Names for variables (and other things) are called identifiers. • Identifiers in C++ conform to the following rules: – A variable name must begin with a letter or the underscore character. – The name must not be one of C++’s reserved words: – Identifiers should make their purpose obvious to the reader. – Identifiers should adhere to standard conventions. Variable names, for example, should begin with a lowercase letter. asm bool break auto case catch char class const continue default do double else enum false friend using float for goto if inline log mutable int signed long sizeof new unsigned operator private protected public return short static struct union switch template this throw throws typedef true try void volatile while
上海交通大学交大密西根 联合学院· UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Variable Declarations In C++,you must declare a variable before you can use it.The declaration establishes the name and type of the variable and,in most cases,specifies the initial value as well. The most common form of a variable declaration is type name value; where type is the name of a C++primitive type or class,name is an identifier that indicates the name of the variable,and value is an expression specifying the initial value. Most declarations appear as statements in the body of a function definition.Variables declared in this way are called local variables and are accessible only inside that method. Variables may also be declared as part of a class.These are called instance variables
Variable Declarations Variable Declarations • In C++, you must declare a variable before you can use it. The declaration establishes the name and type of the variable and, in most cases, specifies the initial value as well. • The most common form of a variable declaration is • where type is the name of a C++ primitive type or class, name is an identifier that indicates the name of the variable, and value is an expression specifying the initial value. • Most declarations appear as statements in the body of a function definition. Variables declared in this way are called local variables and are accessible only inside that method. • Variables may also be declared as part of a class. These are called instance variables. type name = value ;
上海交通大学交大密西根 ·联合学院一 81 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Operators and Operands As in most languages,C++programs specify computation in the form of arithmetic expressions that closely resemble expressions in mathematics. The most common operators in C++are the ones that specify arithmetic computation: Addition ★ Multiplication Subtraction Division 号 Remainder Operators in C++usually appear between two sub expressions, which are called its operands.Operators that take two operands are called binary operators. The -operator can also appear as a unary operator,as in the expression -x,which denotes the negative of x
Operators and Operands Operators and Operands • As in most languages, C++ programs specify computation in the form of arithmetic expressions that closely resemble expressions in mathematics. • The most common operators in C++ are the ones that specify arithmetic computation: • Operators in C++ usually appear between two sub expressions, which are called its operands. Operators that take two operands are called binary operators. • The ‘-’ operator can also appear as a unary operator, as in the expression -x, which denotes the negative of x. + Addition – Subtraction * Multiplication / Division % Remainder