Skip to content
wannacode

Your first program

Problem

The computer does exactly what it says

The computer does not guess what you want. It runs commands, one after another, exactly as they are written. A sequence of such commands is a program.

Here is a whole program. It prints two lines on the screen:

Program.cs
class Program
{
static void Main()
{
Console.WriteLine("Welcome!");
Console.WriteLine("This is your first program.");
}
}
Output
Welcome!
This is your first program.

It looks like a lot of code for two lines of text. Most of it is a wrapper that you write in every program. Your commands are only the two lines in the middle.

The idea

What is in the program

  • static void Main() is the place where the program starts. We call it Main. The computer runs the lines in it from top to bottom.
  • Console.WriteLine(...) prints the text in the parentheses and moves to a new line.
  • The text is in double quotes: "Welcome!". The quotes are not printed.
  • Every command ends with ;.
  • The curly braces { and } group the lines that belong together.

class Program is the wrapper of every program. You will learn what it means in the "Classes and objects" module. For now you copy it and write your code inside Main. Later in the lessons we show only the lines from Main.

More precisely, if you are curious
  • Uppercase and lowercase letters matter. console.writeline is not the same as Console.WriteLine, and the program will not compile.
  • The command dotnet new console creates Program.cs with just one line: Console.WriteLine("Hello, World!");. That is a short way to write the same thing. The compiler adds class Program and Main around it by itself. Here we write the full form, so you can see where everything goes.
  • In older code you will see using System; at the very top. It says where Console comes from. In new projects this line is added for you.

The idea

Printing with and without a new line

Sometimes you want several pieces of text on one line. There is a second command for that: Console.Write. It prints the text and stays on the same line.

Console.Write("Order ");
Console.Write("#1024");
Console.WriteLine(" is confirmed.");
Console.WriteLine("Delivery: tomorrow");
Output
Order #1024 is confirmed.
Delivery: tomorrow

The first two lines of the code use Write, so "Order", "#1024" and "is confirmed." come out together. After WriteLine the next text starts on a new line.

Pay attention to the spaces. They are inside the quotes, because the computer does not add a space by itself.

Follow the cursor: the text always comes out where it is.

Console.Write("Order ");
Console.Write("#1024");
Console.WriteLine(" is confirmed.");
Console.WriteLine("Delivery: tomorrow");
Step by step, in words
  1. Write prints "Order " and the cursor stays on the same line.
  2. Write prints "#1024" and the cursor stays on the same line.
  3. WriteLine prints " is confirmed." and the cursor moves to a new line.
  4. WriteLine prints "Delivery: tomorrow" and the cursor moves to a new line.
  5. The spaces are inside the quotes: the computer does not add a space by itself.

Try it

On how many lines?

What will the program print?

Console.Write("Train ");
Console.WriteLine("8:15");
Console.Write("Platform ");
Console.Write("3");
Console.WriteLine();
Answer

The idea

Comments: notes for people

Sometimes you want to leave a note in the code: why it is like this, or what comes next. You write it as a comment. The computer skips comments.

  • // starts a comment that runs to the end of the line.
  • /* and */ surround a comment over several lines.
/* The coffee shop receipt.
Prices are in euros. */
Console.WriteLine("Coffee €2.50");
// Console.WriteLine("Croissant €1.80");
Console.WriteLine("Total €2.50"); // out of croissants
Output
Coffee €2.50
Total €2.50

The croissant line is code, but it is behind //, so it does not run. This is how you switch off a line for a while without deleting it.

A good comment says why, not what. The code itself shows what it does.

Try it

What will come out?

What will the program print?

Console.WriteLine("9:00 Math");
// Console.WriteLine("10:00 Physics");
Console.WriteLine("11:00 History"); // lunch is next
Answer

Problem

The program does not start

You wrote the program, you run it, and it does not even begin. Instead of output you get a message:

Program.cs
class Program
{
static void Main()
{
Console.WriteLine("Welcome to the course!")
Console.WriteLine("Here we go.");
}
}
Message from the compiler
Program.cs(5,52): error CS1002: ; expected

Before it runs, the program goes through the compiler. It translates the code for the computer and checks that it is written by the rules. If it is not, the compiler stops and does not run anything. This is called a compile error.

The idea

How to read the message

The message tells you three things: the file, the line and what is wrong.

Program.cs(5,52) means the file Program.cs, line 5, character 52 from the start of the line. ; expected means "I expected a ;". CS1002 is the number of the error. You can look it up in the documentation.

The most common errors at the start:

  • a ; is missing at the end of a command;
  • a quote or a parenthesis is opened but not closed;
  • a lowercase letter instead of an uppercase one: console.WriteLine.

Fix the first error in the list first. One missing quote can confuse the compiler and show several more errors further down, which go away along with it.

More precisely, if you are curious
  • The compiler tells you where it noticed the problem. Sometimes the error is a little higher up: if you forgot to close a }, it finds out only at the end of the file.
  • There are also errors that nobody catches: the program starts, but it does something other than what you want. We will hunt those in almost every lesson.

Try it

Find the bug

The program should print the exam schedule. It does not compile. Which line is the bug on?

Pick the line with the bug.

Exercise

Your first programs

Solve the tasks in your editor. Each one is a separate program. Compare your output with the sample, character by character.

1. Hello World. The traditional first program. Print:

Output
Hello, World!

2. Business card. Print a business card of three lines: name, job and email. You can use your own.

Sample output
Maria Ivanova
web developer
maria@example.com

3. Frame. Print today's cafeteria menu in a frame of asterisks. Watch the spaces, so the right side comes out straight.

Output
***************
* Today: soup *
* €5.40 *
***************

More tasks

  • One sentence in three parts. With three Write commands and one WriteLine, print on one line "The shop is open from 9:00 to 19:00."
  • A schedule with a comment. Print a schedule of four classes. Then switch off the third line with //, without deleting it, and add a comment saying why.
  • An error on purpose. Remove the ; from a line in task 1, then remove a quote as well. Read what the compiler says each time.

Test

Check yourself

1. Where does the program start running?
2. What is the difference between Write and WriteLine?
3. What does the computer do with a comment?
4. The compiler says that ; is missing on line 3. What does that mean?
5. Which one will compile?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·