Presentation is loading. Please wait.

Presentation is loading. Please wait.

Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal 6.3333.

Similar presentations


Presentation on theme: "Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal 6.3333."— Presentation transcript:

1 Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal 6.3333 floating point literal 6.0 floating point literal 6E7 floating point literal 'a' character literal “Simon” String literal true bool literal

2 Constants It is often useful to name literals, that is, to use symbolic names rather than the literals directly. The const modifier is used to declare a data object whose value cannot be changed once it is initialized (and so it must be initialized to set the original value) const double PI = 3.1415926;

3 String Literals String literals are sequences of characters enclosed in double quotes ( “ ). This is different from char literals which consist of a single character enclosed in single quotes ('). You can put a double quote inside of a string by using an escape sequence, that is, preceding the double quote by a backslash. Example: “This is a double quote \”.”

4 Assignment Compatibility A data object can only hold one type of object (int, float, double, char, string), but sometimes you can convert an object of one type to another type before storing it. For example, double a = 4; is perfectly fine. 4 is converted to a double, 4.0, before being stored in a. int b = 3.6; is not okay.

5 Shorthand Assignment Statements In C++ you can abbreviate an assignment statement which modifies the variable on the left hand side. For example, a += 5; is shorthand for a = a + 5; Similarly, *=, -=, /= are shorthand for their respective operations

6 Integer Division One thing to watch out for is integer division. If you divide two ints, the result is another int, not a floating point number. So, 19/4 is 4, not 4.75. If you divide two floating point numbers, the result is a floating point number: 19.0/4.0 is 4.75. If you divide an int by a floating point number, the int is converted to floating point first.

7 Increment and Decrement C++ provides shorthand operators for incrementing and decrementing values. n++; increments the value of n by 1 and the value is n before the increment ++n; increments the value of n by 1 and the value is n after the increment n-- and --n are similar, but decrement the value.

8 Output The variable cout represents the output stream, that is, normally the terminal. You can send stuff to the output stream by using the << operator. These can be chained together to print out different items. For example, cout << “The total of “ << a << “ and “ << b << “ is “ << a + b << endl; The constant endl prints out a new end of line character.

9 Input The variable cin represents standard input, that is, normally the keyboard. You can use the operator >> to read in and set variables. For example, int a, b; cin >> a >> b; will read in two int s and store them in a and b.

10 Exercise Write a C++ program that will prompt the user for a temperature (in Fahrenheit) and then convert that temperature to Celsius and print out the new temperature along with a text message. You will need: A double variable, f, to represent the Fahrenheit temperature A double variable, c, to represent the Celsius temperature

11 Exercise (cont'd) An output line to print out the prompt message An input line to read in the Fahrenheit temperature A line to calculate and store the Celsius temperature (c = (f – 32) * 5/9. A line to print out the result.


Download ppt "Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal 6.3333."

Similar presentations


Ads by Google