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
WriteLineon 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.
Summary
This lesson presents a hands-on C# programming exercise based on displaying text in the console. The goal is to display the message "hello, the weather is really nice today" on a single line, despite two separate display calls, then add on the next line the text "it's a good day to go do some sport." The exercise aims to build mastery of line-break handling and display concatenation.
Key points
- Move from theory to practice with a console display exercise
- Display several texts on a single line with no intermediate line break
- Handle line breaks to move to the next line with a second message
- Correctly use the display functions (Console.Write vs Console.WriteLine)
- Understand that two separate display calls can produce continuous text
- Master text output formatting in C#
FAQ
How do I display two texts on the same line in C#?
By using two successive display calls without a line break between them (for example, two Console.Write() calls), the two texts are displayed consecutively on the same line.
How do I move to a new line in C#?
By using the Console.WriteLine() method, which automatically adds a line break after the text, or by inserting an explicit line break into the text itself.
What is the main goal of this exercise?
To practice and master console display in C#, in particular managing line breaks and correctly formatting the text output of several display calls.