C-SHARP - 1.4 My first program (1/2)

This exercise puts console display into practice after the first theory videos. The goal: display "Hello, the weather is really nice today" on a single line but using two different functions, then on the next line "Yes indeed, it's a good day to go do some sport." Pause the video, code it yourself, then compare with the walkthrough that follows.

Walkthrough

Two functions are at the heart of this: Console.WriteLine(), which displays text and then moves to a new line, and Console.Write(), which displays text without moving to a new line. To make the first sentence fit on a single line while still being produced by two separate calls, we use Console.Write for the first half and Console.WriteLine for the second half (which takes care of the final line break).

Console.Write("Hello, ");
Console.WriteLine("the weather is really nice today");
Console.WriteLine("Yes indeed, it's a good day to go do some sport");
  • The space after the comma in "Hello, " is crucial: a computer doesn't guess that a space needs to be inserted between two concatenated strings
  • Without that space, the two sentences would run together (Hello,the weather...)
  • The WriteLine on the second line triggers the line break needed to display the third sentence underneath
  • The semicolon closes each statement

Run the program with the Play button: the output window displays the first sentence on a single line, then the second one underneath. This exercise illustrates two important notions: the difference between Write and WriteLine, and the need to manually manage spacing when concatenating strings. That's it for this walkthrough, see you very soon in the next lessons.