Console.Write("Press a key followed by ENTER: ");
// Read a key from the keyboard.
ch = (char) Console.Read();
Console.WriteLine("Your key is: " + ch);
}
}
Here is a sample run:
Press a key followed by ENTER: t
Your key is: t
The fact that Read( ) is line-buffered is a source of annoyance at times. When you press
ENTER, a carriage-return, linefeed sequence is entered into the input stream. Furthermore, these
characters are left pending in the input buffer until you read them. Thus, for some applications,
you may need to remove them (by reading them) before the next input operation. You will see
an example of this later in this chapter.
The if Statement
Chapter 1 introduced the if statement. It is examined in detail here. The complete form of the
if statement is
if(condition) statement;
else statement;