Hello, World!
In the previous lesson, a program was a recipe the computer follows from top to bottom. Now let's take apart one step of such a recipe and see how to print several lines.
Here is the one-line program again:
Console.WriteLine("Hello, World!"); // Hello, World!Let's take the line apart, from left to right:
Consoleis the name of the console: the text window where a program prints and reads text. It is the same terminal window where you typeddotnet run.- The dot after
Consoleopens what the console can do, andWriteLineis one of its actions: "write a line". - The parentheses hold what to print. The action's name together with its parentheses is called a call: that is how a program asks the console to do something.
"Hello, World!"is text in double quotes, a string literal. The program prints it as it is, without the quotes.- The semicolon
;ends the step.
Everything from Console to ; is one statement, that is, one step of the recipe. The file holds nothing but that statement, and that is enough: such a file is already a whole program.
Statements run in order
A program can have any number of statements. The computer runs them one after another, from top to bottom, and the next one starts only when the previous one has finished:
Console.WriteLine("Heat the pan"); // Heat the pan
Console.WriteLine("Add the oil"); // Add the oil
Console.WriteLine("Fry an egg"); // Fry an eggSwap two lines, and they swap in the output too: the computer runs the statements in the order they are written and looks for no other. Here the recipe and the program part ways. An experienced cook chops an onion while the pan heats up, but a program like this one runs strictly one statement at a time.
Statements are usually written one per line, so they are comfortable to read. The compiler, though, goes by the semicolons, not by line breaks: two statements on one line work too, as long as each ends with its own ;.
WriteLine and Write
There are two ways to print to the console. They differ in one thing only: what happens after the text.
WriteLine ends the line
Console.WriteLine prints its text and ends the line: the next output starts at the beginning of a new line. The call Console.WriteLine() with empty parentheses prints nothing and only ends the line.
Write keeps the line open
Console.Write prints its text and leaves the cursor on the same line. The cursor is the place where the next character will appear, so the next output continues that line:
Console.Write("Hello, ");
Console.Write("World");
Console.WriteLine("!"); // Hello, World!
Console.WriteLine("Bye"); // ByeLet's see what each statement does:
Console.Write("Hello, ")printsHello,together with the space at the end, and the cursor stays on the first line.- The second
WriteaddsWorldright after the space. WriteLine("!")adds the exclamation mark and ends the line: the first line of output is done, and it readsHello, World!.- The last statement prints
Bye, already on the second line.
The space between the words sits in the first literal itself. Neither Write nor WriteLine adds anything between outputs: they print exactly what is inside the quotes.
In the widget below, a pancake recipe is printed step by step. Press "Next step" and watch the "output" line: after Write the text stays on the same line, and after WriteLine the next output starts on a new one.
1Console.WriteLine("Pancakes");2Console.Write("1. Mix ");3Console.Write("flour and milk");4Console.WriteLine(".");5Console.Write("2. Fry");6Console.WriteLine();7Console.WriteLine("Enjoy!");
WriteLine prints "Pancakes" and ends the line.
The order here is the same as in a recipe: each statement runs after the previous one and prints its piece where the cursor stopped. Where a line ends is decided only by the choice between Write and WriteLine.
Comments
A comment is a note for people, and the compiler skips it. C# has two kinds:
//starts a comment that runs to the end of the line;/*and*/enclose a comment that may span several lines.
A comment is a handy way to switch a statement off for a while: add // at the start of the line, and the compiler skips it.
Common mistake
Two words are printed by two calls, and the space is forgotten:
Console.Write("Hello,"); // ✗
Console.WriteLine("World");The program compiles and prints Hello,World—the words run together. Write adds no space after its text, and WriteLine adds none before its text. The space goes inside the literal, as in the example above: Console.Write("Hello, ");.
Key points
- A program's statements run one after another, from top to bottom, and each ends with a semicolon.
Console.WriteLineprints text and ends the line, andConsole.WriteLine()with empty parentheses only ends the line.Console.Writeprints text and leaves the cursor on the same line, so the next output continues it.- Neither
WritenorWriteLineadds spaces: a space goes inside the string literal.