Pseudo Code To C# Tutorial

Hello and welcome to this tutorial. In this tutorial we will see how to write a pseudo code and how we can convert it to c# code.

 

First, let’s talk about pseudo code. The pseudo code is to write your code in regular language and then convert it to a working code. So, instead of going directly too the IDE and write a code, you can use a paper and a pen and write down what you want the code to do. This can help you identify what are the requirement for this task (No. of input control, conditions and what the result of them) before writing the original code.

Now, let’s take an example about pseudo code and how we can write it.

Example 1: Write a code to take an integer number, and print a message if it’s odd or even:

Solution:

Start

Declare Variable x

Input Integer Number and set x equal to that number

If x mod 2 equals zero

Show message Even

Else

Show message Odd

End If

End

 

Now, as you can see we did not write a code for a specific language. Instead, we used words to describe the wanted operation to be executed by the program.

Let’s take a much complex example and see how we can write it in pseudo code.

Example 2: Write a code to insert data for a new employee and check it’s experience and in which department. Then display the suggested position and salary. Remember that the range for salary is: 5 years or less 2000 and a normal worker, between 5 and 10 3500 and a head of a department, above 10 5000 and a manager. After that, check if the employee have an existing known colleges in the company

Solution:

Start

Enter Employee Data & Contacts

If Employee Experience < 5

Show position worker and a salary of 2000

ELSE If Employee Experience > 5 and <10

Show position head of a department and a salary of 3500

ELSE If Employee Experience > 10

Show position head of a department and a salary of 5000

End If

For each contact for employee

check existing employees if he is already known to them

End for each

End

 

Now, in the above two examples we don’t have a specific roles to write the pseudo code. we can use what ever expression we would like to describe or write the actions we want. No rules are giving to write that code.

Now, we we write pseudo code? We write it for many reasons:

  1. Identify how we can code our problem.
  2. Define the condition and checks.
  3. Sharing the problem for different platforms (If we want to create an Android and iOS apps, they won’t use the same programming language, we use pseudo code to share the problem in a simplified way between them)

Converting the pseudo code to C# code

Now, let’s look at the following example and see how we can convert it to an actual c# program

 

Example 3: Write a program to take a student data and analyze it to tell if he finished his university or still he needs more credits to finish. There are three majors of study: Science, IT, Engineering. The Science needs 139 credits, the IT need 142, the Engineering needs 150 credits to graduate. If he has the required credits in the selected field then he can graduate.

Solution: We will create a windows forms application so that we execute the code inside it. Let’s first write the pseudo code:

Start

Enter Student name

Select Student Major

Enter Student Total Credit

if Major is Science

If Credit = 139 Then Show Message Can Graduate Else Show Message Need More Credits (Subtract 139 of Number of Credits)

Else If Major is IT

If Credit = 142 Then Show Message Can Graduate Else Show Message Need More Credits (Subtract 142 of Number of Credits)

Else If Engineering is IT

If Credit = 150. Then Show Message Can Graduate Else Show Message Need More Credits (Subtract 150 of Number of Credits)

End If

End

Now, if we look at the pseudo code we need to select a major for the student, we can make the major as a drop down so the user select from them the major. Also, we need a textbox so we insert his name. And another textbox to insert his total credit. The pseudo code lowered the needed time to think about how we will write our code and also what needed of input fields.

This is the UI for the windows form application

 

 

And this is the code for the application button:

 

Snippet

2 thoughts on “Pseudo Code To C# Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *