Learn to program in the D programming language

Cours Learn to program in the D programming language, tutoriel & guide de travaux pratiques en pdf.

Standard Input and Output Streams

So far, the printed output of our programs has been appearing on the console window (or screen). Although this is actually the case with many uses of many programs, in reality the characters are printed to standard output streams of programs.
The standard output is character based; everything to be printed are first converted to their character representations and then sent to the output one by one as characters. For example, the integer value 100 that we’ve printed in the last chapter is not sent to the output as the value 100, but as the three characters 1, 0, and 0.
Similarly, what we normally perceive as the keyboard is actually the standard input stream of a program and is also character based. The information always comes as characters to be converted to data. For example the integer value 42 actually comes through the standard input as the characters 4 and 2.

These conversions happen automatically

This concept of consecutive characters is called a character stream. As D’s standard input and standard output fit this description, they are character streams.
The names of the standard input and output streams in D are stdin and stdout, respectively.
Operations on these streams normally require the name of the stream, a dot, and the operation; as in stream.operation(). Because stdin and stdout are used very commonly, as a convenience, standard operations on them can be called without the need of the name and the dot, as in operation().
writeln that we’ve been using in the previous chapters is actually the short for stdout.writeln. Similarly, write is the short for stdout.write. Accordingly, the hello world program can also be written as the following:
import std.stdio;
void main()
{
stdout.writeln(« Hello world! »);
}

Exercise

• Observe that stdout.write works the same as write.
… the solution

Reading from the Standard Input

Any data that is read by the program must first be stored in a variable. For example, a program that reads the number of students from the input must store this information in a variable. The type of this specific variable can be int.
As we’ve seen in the previous chapter, we don’t need to type stdout when printing to the output, because it is implied. Further, what is to be printed is specified as the argument. So, write(studentCount) is sufficient to print the value of studentCount.
Skipping the whitespace characters
Even the Enter key that we press after typing the data is stored as a special code and is placed into the stdin stream. This is useful to the programs to detect whether the information has been input on a single line or multiple lines.
Although sometimes useful, such special codes are mostly not important for the program and must be filtered out from the input. Otherwise they block the input and prevent reading other data.

Logical Expressions

Actual work that a program performs are accomplished by expressions. Any part of a program that produces a value or a side effect is called an expression. It has a very wide definition, because even a constant value like 42 and a string like « hello » are expressions because they produce the respective constant values 42 and « hello ».
Note: Don’t confuse producing a value with defining a variable. Values need not be associated with variables.
Function calls like writeln are expressions as well, because they have side effects. In the case of writeln, the effect is on the output stream by the placement of characters on it. Another example from the programs that we have written so far would be the assignment operation, which effects the variable on its left-hand side.
Because of producing values, expressions can take part in other expressions. This allows us to form more complex expressions from simpler ones. For example, assuming that there is a function named currentTemperature() that produces the value of the current air temperature, the value that it produces may directly be used in a writeln expression.

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *