|
Data, Variables and Expressions
Part:
1
2
3
4
Data Literals
The main goal of a computer programming language is to help human to write instructions
process data. So the first thing to learn about a computer language is to know how data is
represented in this language.
In C#, data is divided into 5 different types:
- Boolean
- Integer
- Real
- Character
- String
When a value of any data type is entered directly into a C# program, it is called a literal.
The following rules defines some basic formats of how literals of different data types can be entered
into a C# program. We will introduce more special formats later.
Rule: There are only two boolean literals. They are the same as the English
words: true, false.
Rule: Integer literals can be entered in a decimal format. For example:
0, 555, -777, 911.
Rule: Real literals can also be entered in a decimal format. For example:
0.0, 555.0, -777.0, 3.14159.
Rule: Character literals can be entered within two single quote characters. For example:
'a', 'A', '0', '9'. This format is fine to enter regular characters. But there are also some special
printing commands that are treated as special characters, like backspace, tab, return and form feed.
Rule: Character literals for special characters can be entered as an escape sequence within
two single quote characters. For example: '\t', '\r', '\n', '\'', '\"', '\\'.
Rule: String literals can be entered within two double quote characters. Escape sequence
is allowed to represent special characters. For example: "911", "Hello world!", "Joe said \"Hello\" to me".
In order to experiment what we have learned in this section, we need to learn one more thing about
the WriteLine method we used in our first program. The WriteLine method can take more than one parameter
using the following format:
WriteLine("...{0}...{1}...{2}...", literal_1, literal_2, literal_3);
In this format, the first parameter is the formatting string which contains place holders like {0}, {1}
and {2}. The second parameter and any additional parameters can be any types of literals.
Each place holder has an index number, and it will be replaced by the corresponding literal while
the method is executed. During the replacement process, each literal will be converted into
a string first. Then it will be placed into the formatting string to occupy the place holder's place.
Note that the index number of place holder starts with 0, not 1.
Now let's apply what we have learned so far, and write the following experimental program with
literals.cs as the file name:
class Literals {
public static void Main() {
System.Console.WriteLine("Boolean literals: {0} and {1}.", true,
false);
System.Console.WriteLine("Integer literals: {0}, {1}, {2}, {3}.",
0, 555, -777, 911);
System.Console.WriteLine("Real literals: {0}, {1}, {2}, {3}.", 0.0,
555.0, -777.0, 3.14159);
System.Console.WriteLine("Character literals: {0}, {1}, {2}, {3}.",
'a', 'A', '0', '9');
System.Console.WriteLine("Character literals: {0}, {1}, {2}, {3},
{4}.", '\t', '\r', '\n', '\"', '\\');
System.Console.WriteLine("String literals: {0}, {1}, {2}.", "911",
"Hello world!", "Joe said \"Hello\" to me.");
}
}
After compiling and executing the program, we will get the following output:
Boolean literals: True and False.
Integer literals: 0, 555, -777, 911.
Real literals: 0, 555, -777, 3.14159.
Character literals: a, A, 0, 9.
, aracter literals: ,
, ", \.
String literals: 911, Hello world!, Joe said "Hello" to me..
There are a couple of interesting things to be noted here:
- The boolean literal "true" is converted to a string with an upper case "T".
- The real literal "0.0" is converted to a string with the trailing ".0" removed.
- The character literals for special commands are executed while being displayed on
the screen. The tab command '\t' pushed the next character ',' to the next tab position with
several space characters. The return command '\r' pushed the next character back to the beginning
of the line and whipped out the original character 'C'. The new line command '\n' forced
the display process to move the next line.
(Continued on next part...)
Part:
1
2
3
4
|