, ean, arithmetic s s on acters Comp Sci 1570 Introduction to C++
Outline s s on acters 1 2 3 4 s s on acters
Outline s s on acters 1 2 3 4 s s on acters
ASCII s s on acters
ASCII s s on acters
Type: acter s s on acters Allocation: 1 byte Range of values: a type variable can take on the values of the ASCII acter set. There are 256 acters in the set, including the digits, uppercase alphas, lower case alphas, punctuation, and many other special acters. Example declaration: c o n t i n u e R e s p o n s e = y ; See demo code.
Escape acters s s on acters
Outline s s on acters 1 2 3 4 s s on acters
Type: s s on acters Allocation: varies Example declaration: s t r i n g g r e e t i n g = h e l l o. s t r i n g name ; c i n >> name ; // suppose you type Bob Smith cout << name ; // o u t p u t s Bob ( not Smith ) Use the double quotes for variable initializations, while using the single quotes (or ticks) for acters. Though you can initialize a variable to any, even one that includes spaces, reading into a variable using a cin statement is different. If you use a cin statement to read a value into a variable, it will only read up to the first space and then stop. We ll show a solution later. See demo code.
Outline s s on acters 1 2 3 4 s s on acters
Type: Boolean s s on acters Allocation: 1 byte Possible values: true or false Example declaration: q u i t = f a l s e ; passed = true ; true and false are reserved, which means is that they (and many others) have special meanings to the compiler and cannot be used as identifiers such as variable names. variable can be used as a primitive if statement. See demo code.
Outline s s on acters 1 2 3 4 s s on acters
Constants s s on acters Declaring a variable as ant will tell the compiler not to allow it to be changed. A ant may NOT be on the left hand side of an assignment statement. f l o a t PI = 3. 1 4 1 5 9 ; double DISTORTION COEF = 5. 6 6 2 3 9 8 ; f l o a t TAX RATE =. 0 2 3 ; PI = 4 ; // NO, w i l l not c o m p i l e Benefits of CONSTANTS variables can be protected from inadvertent corruption. In addition, the variable name identifies them, taking the mystery out of an unidentifiable number. Perhaps a value in your program needs to be changed at some point in time. It is easy to change the value of a ant in one location rather than have to search perhaps thousands of lines of code to change all instances.
When to use ants s s on acters short TWO = 2 ; f l o a t val1, val2, a v e r a g e ; a v e r a g e = ( v a l 1 + v a l 2 ) / TWO; short TWO = 3 ; f l o a t val1, val2, val3, a v e r a g e ; a v e r a g e = ( v a l 1 + v a l 2 + v a l 3 )/TWO; short DIVISOR = 2 ; See demo code.
Outline s s on acters 1 2 3 4 s s on acters
Names not to use s s on acters There are that are used in the C++ language that should not use in any other way. Thus, you should be tempted to name a variable new, since that is a reserved word. Do not use any of the following for your own named objects: auto double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile do extern if return static union while asm dynamic cast namespace reinterpret cast explicit new static cast typeid catch false try operator template typename class friend private this using cast inline public throw virtual delete mutable protected true w t and bitand compl not eq or eq xor eq and eq bitor not or xor cin endl INT MIN iomanip main npos std cout include INT MAX iostream MAX RAND NULL
Outline s s on acters 1 2 3 4 s s on acters
Outline s s on acters 1 2 3 4 s s on acters
s s on acters Symbol used for assignment of a value to a variable is = tax = income RATE; // RATE i s c o n s t RATE =. 0 5 ; // not a l l o w e d! 4 = income + 6 4 ; // not a l l o w e d! The compiler goes through these steps to execute: 1 Value of the rhs (right hand side) is determined. In this case, memory locations for income and RATE have to be read, and then those values multiplied. 2 Types of the rhs and lhs are determined. If the type of the lhs can be converted to the type of the rhs without losing information, then it is done. If information would be lost in doing so, a compiler warning is issued. 3 Value of the rhs is copied into the memory location of the lhs. Thus, the lhs MUST BE A MODIFIABLE MEMORY LOCATION. That is, lhs cannot be a ant.
Outline s s on acters 1 2 3 4 s s on acters
s s on acters +,,, /, % ( modular a r i t h m e t i c ) somevalue = num1 + num2 ; // a d d i t i o n somevalue = num1 num2 ; // s u b t r a c t i o n somevalue = num1 num2 ; // m u l t i p l i c a t i o n somevalue = num1 / num2 ; // d i v i s i o n somevalue = num1 % num2 ; // modulus
A=10; B=20; s s on acters
Division s s on acters i n t hour, minute ; hour = 1 1 ; minute = 5 9 ; cout << Number o f minutes s i n c e midnight : ; cout << hour 60 + minute << e n d l ; cout << F r a c t i o n o f the hour passed : ; cout << minute /60 << e n d l ; See code demo. What does this output?? More to come next class
Outline s s on acters 1 2 3 4 s s on acters
s s on acters Multiplication and division happen before addition and subtraction. So 2*3-1 yields 5, not 4, and 2/3-1 yields -1, not 1 (remember that in integer division 2/3 is 0). If the have the same they are evaluated from left to right. So in the expression minute*100/60, the multiplication happens first, yielding 5900/60, which in turn yields 98. If the operations had gone from right to left, the result would be 59*1 which is 59, which is wrong. Any time you want to override the rules of (or you are not sure what they are) you can use parentheses. Expressions in parentheses are evaluated first, so 2 * (3-1) is 4. You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60, even though it doesn t change the result.
Associativity s s on acters To see how associativity works, consider the expression 2-3 - 4 The two are the same, so they have equal. Should the first subtraction operator be applied before the second, as in (2-3) - 4 (that is,-5), or rather is 2 - (3-4) (that is, 3) the correct interpretation? The former (-5) is the correct interpretation. We say that the subtraction operator is left associative, and the evaluation is left to right. This interpretation agrees with standard arithmetic rules. All binary except assignment are left associative.
Associativity s s on acters Assignment is an exception; it is right associative. To see why associativity is an issue with assignment, consider the statement w = x = y = z; This is legal C++ and is called chained assignment. Assignment can be used as both a statement and an expression. The statement x = 2; assigns the value 2 to the variable x. The expression x = 2 assigns the value 2 to the variable x and evaluates to the value that was assigned; that is, 2. Since assignment is right associative, the chained assignment example should be interpreted as w = (x = (y = z)); which behaves as follows: The expression y = z is evaluated first. z s value is assigned to y, and the value of the expression y = z is z s value.
s s on acters
Outline s s on acters 1 2 3 4 s s on acters
s on acters? s s on acters #i n clude <i o s t r e a m > #i n clude <s t r i n g > using namespace s t d ; i n t main ( ) { l e t t e r ; l e t t e r = a + 1 ; cout << l e t t e r << e n d l ; //?? cout << l e t t e r + b << e n d l ; //?? l e t t e r = a + b ; cout << l e t t e r << e n d l ; //?? } s t r i n g word ; word = h e l l o ; cout << word + world << e n d l ; //?? cout << word world << e n d l ; //?? cout << word world << e n d l ; //?? cout << word / world << e n d l ; //??