C# Statements
Statements
A computer program is a list of instructions for the computer to carry out (execute).
In a programming language, these instructions are called statements.
The following statement tells the computer to print the text "C# is fun!" to the screen:
Example
Console.WriteLine("C# is fun!");
Every statement ends with a semicolon ;.
If you forget the semicolon, you get an error, and the program does not run:
Example
Console.WriteLine("C# is fun!")
The compiler shows an error like this:
prog.cs(10,5): error CS1002: ; expected
CS1002: ; expected means "I expected a semicolon here". The numbers (10,5) are the line and the position where C# noticed that something was missing.
Many Statements
Most C# programs contain many statements.
The statements run one by one, in the same order as they are written:
Example
Console.WriteLine("Hello World!");
Console.WriteLine("Have a good day!");
Console.WriteLine("Learning C# is fun!");
Example explained
The example above has three statements:
Console.WriteLine("Hello World!");Console.WriteLine("Have a good day!");Console.WriteLine("Learning C# is fun!");
The first statement runs first and prints "Hello World!".
Then the second statement prints "Have a good day!".
At last, the third statement prints "Learning C# is fun!".