Introduction to programming with C#

Programming is rather simple, yet lots of people think programmers are casting spells using keyboards. This tutorial aims at demystifying programming and introducing basic programming concepts to novices.

What is Programming?

Programming is providing something with instructions for performing a task automatically. There are different styles of programming. This tutorial will focus on giving step-by-step description of how a program should operate. This is called Imperative Programming.

How do you get an elephant in a fridge?

This famous riddle was actually part of an interview. While thinking of the size the elephant can be distracting, the answer to this question is straightforward:

  1. Open the fridge
  2. Put the elephant
  3. Close the fridge

As you can see, these are just simple steps. Programming takes a similar approach.

For exercises, note that my solutions may be different from the ones you come up with. It does not mean that yours are bad, if the outcome is the same, the problem is solved.

What is a programming language?

Computers do not understand natural languages(English, French etc..). To specify the instructions, we have to use their language (machine codes: 0 and 1). To make it more readable for programmers, several programming languages that resembles were created. The instructions are then converted to machine codes using a compiler.

This tutorial will use the C# programming language. It is an Object Oriented Language by Microsoft and may be used to make video-games (using for example Unity Game Engine) or other kinds of software. C# development is usual done using Visual Studio but if you do not want to install anything you may use .NET Fiddle. It is an online compiler which is really easy to use.

Coding

Code Structure

For this tutorial, all our codes will have the same basic structure:

  • We will write between the brackets of Main like this:
    • “public static void Main(){<Codes>}”
  • All statements will end with a “;”

You do not worry about the rest.

Hello World!

Our first program will output “Hello Word!” to the screen.

To write to the console screen, we use “Console.WriteLine(<String>)”

To specify what you want to write, we replace <String> with the text enclosed by double quotes(“) as follows:

Comments

Comments are texts that are not converted by the compiler. They can be used to give more information about the codes and can be written in any language.

There are two ways to write comments:

  1. //at the beginning of a line
  2. /*

To enclose multiple lines

*/

Variables

Variables are used to hold values such as Numbers, Letters etc. Those values can then be “plugged” into equations using only the variable name.

In C# variables need to be declared before they are used like this:

  • <Type> <Name>; //Example: int number;

They can also be assigned an initial value like this:

  • <Type> <Name> = <Value>; //Example: int number = 1;

String – Datatype

A String represents a string of characters. It can be used to hold messages and is usually enclosed by double quotes(“).

In the previous example “Hello World“ was a String. Before outputting the message, it could have been saved in a variable like this:

Exercise 1: Output “Hello” with your name to the screen.

Change the value of the string to include your name.

Possible solution:

Input a String

A program can also accept a String from the user. In C# we use “Console.ReadLine()” to read the next line as a string from the console.

The program below prompts the user for a name and then tells a personalised Hello:

Notes:

  • “Console.Write(<String>)” writes the string to the Console but does not leave a line.
  • ‘+’ can be used to add a string at the end of another.

Exercise 2: Prompt the user for name. Output name and age.

Change the code so that the program asks for a name and include your age in the output string.

Possible solution:

Integer – Datatype

An Integer represents a whole number. It can be used for mathematical calculations and does not need any enclosure.

Integers can be declared using different datatype names (int, Int32, etc…) which represent different sizes of Integer.

The program below calculates the value of age + 3 and outputs it to the screen:

Converting String to Integer

When we use “Console.ReadLine()”, it returns a String. Strings are not directly converted to Integers. To do the conversion we can use this “Convert.ToInt32(<String>)”.

The program below prompts the user for an age and outputs the age in 3 years:

Warning: If the String is not a number, the program will crash!

Exercise 3: Prompt for Name and Age. Output Name and Age in 7 years.

Change the code so that the program asks for a name and an age and outputs on one line the name and the age of the user in 7 years.

Possible solution:

Boolean – Datatype

A Boolean represents one of 2 values(True or False, 1 or 0, etc..) It can be used to hold comparison results.

The program below outputs the value of true and false:

Comparison Operators

Comparison operators compare 2 values and return a Boolean. There are several types of comparison operators as shown below:

  • ==  Equal to
  • !=   Not equal to
  • <    Less than
  • >    Greater than
  • <=  Less than equal to
  • >=  Greater than equal to

The program below prompts the user for a name and an age, compares those value with “Aber” and 10 respectively and outputs the results:

Exercise 4:  Prompt for Country and Money. Output whether foreigner or rich.

Change the code so that the program asks a country and an amount of money and outputs whether it is true or false that the user is a foreigner(not from Mauritius) or rich(more than 200000).

Possible solution:

if statement – Condition

if statements execute certain lines of code based on a Boolean value. The lines will be executed only if the value is true. if statements are used when the lines need to be executed only under certain conditions.

Syntax for if is:

  • if (<Condition>) {<Codes>}

The program below prompt the user for a name and outputs access denied if the name is not “Aber”:

Exercise 5: Prompt for Age; Output “too old” if Age >30

Change the code so that the program asks for an age and outputs “You are too old” if the age is more than 30.

Possible solution:

if…else

We can also specify lines of code to be executed when condition returns false. This is done using the else keyword.

The syntax for if…else is:

  • if (<Condition>) {<Codes>} else {<Codes>}

The program below prompt the user for a name and outputs access denied if the name is not “Aber”. It the name is “Aber” the program outputs “Welcome!”

Exercise 6: Prompt for Age; Output “too old” if Age >30 else Output “right age”

Change the code so that the program asks for an age and outputs “You are too old” if the age is more than 30. If the age is not more than 30 the program should output “You have the right age”.

Possible solution:

while statement – Loop

while statements repeatedly execute certain lines of code based on a Boolean value. The lines will be executed repeatedly only while the value is true. while statements are used when the lines need to be executed several times.

 

The syntax for while is:

  • while (<Condition>) {<Codes>}

The program below outputs all numbers form 0 to 9:

Be cautious not to run into an infinite loop(for example by turning the Boolean value to false under certain condition)

Exercise 7: Output all the Even numbers from 0 to 9.

Write a program which will output all the Even numbers from 0 to 9.

Possible Solution:

Challenge yourself

Exercise 8

Write a program which will:

  • Repeatedly prompt for a number until 0 is input.
  • Output the sum of those numbers
  • Output “WoW that’s a lot!” if the sum is greater than 100
  • Output “That’s nothing” if the sum is less than 10

 

Possible solution:

 

This post was created from this presentation. You can find all the codes used here.