Structured Programming 101

in #csharp-forbeginners6 years ago (edited)

Structured Programing 101

When beginning to learn programing in a structured language, perhaps the first thing that should be taught are structures.
C#, VB.Net, Javascript in fact almost all programming languages are now what is known as Structured Programming languages that is is assumed that the student is fully cognizant of what the structures are. Or in many cases the definitions of structures are just overlooked. I want to start with introducing you to structures. Presently there are only three types of structures; they are:

  • Sequential
  • Branching
  • Looping

All programming code that you write will be one of these structures. So what are they.

Sequential Structures contain code that causes calculations, creates instances of objects etc. Basically they are lines of code that do not affect the logic flow of your program.

Examples of Sequential code:

string myWord = new string();
myTotal = (1+3) * 4;
private static Dog Fido = new Dog(); 

Branching Structures Cause the logic flow of your program to branch to another part of your code if some condition is met.

Examples of Branching Structures:

If(x==null) {x = new item()};

if(x.value < 1)
{
    x.value = 1;
}
else 
{
    y.value += x.value;
}

switch (caseSwitch)
      {
          case 1:
              Console.WriteLine("Case 1");
              break;
          case 2:
              Console.WriteLine("Case 2");
              break;
          default:
              Console.WriteLine("Default case");
              break;
      }

Looping Structures cause the logic flow to repeat in a loop while, or until a certain condition is true.

Looping Examples:

for(int i = 0; i < 5; i++>)
{
    dosomecode(i);
}

foreach(string Item in Items)
{
    process(Item);
}

do {

    x=x-1;
    dosomething(x);

} while (x > 5)

That's it. All of the code that you probably will ever write will be of one of these three structure types. The next trick in your learning experience is deciding when and where to use these structures.

Lets define a use case that exercises all three structure types.

As an Accountant I want to know if the general ledger balances. What I have to do is loop through all of the transactions in the general ledger and add debits to the debit total and credits to the credits total. At the end I have to compare the two and they must sum up to zero.

    public static boolean TestTrialBalance(list<transaction> Transactions)
    {
        //start out with a fail because we do not know if the transactions balance
        
        //sequential structures
        boolean result = false;
        decimal debits = 0;
        decimal credits = 0;

        //looping structure
        foreach(transaction item in Transactions)
        {
            //branching structure
            if(Transaction.Type == TransactionTypes.Debit)
            {
                //sequential structure
                debits += Transaction.Value;
            } 
            Else
            {
                //sequential structure
                credits -= Transaction.Value;
            }
        }

            //sequential structure
            return (debits + credits)== 0;

    }

You can see how this use case is satisfied by using at least one of each structure type. This is a simplified solution to the use case but it does fully satisfy the use case as written. When you view your programming task in these small combinations of structures, things should not be so intimidating as you learn to write code. I have used C# for my examples here but any of the other languages should use the same structures, only syntax will vary.

I am putting this post out there to see if there is an interest in this basic level of programming. I also invite criticism from my peers who have a good level of code experience.

Brought to you by Malluf Consulting Services

As an aside, and if you haven't already I suggest downloading Visual Studio Community Edition. It is free and is the complete Visual Studio Development Environment. My future posts will be using Visual Studio to create example programs.

Oh, and by the way, this is maybe my second article on steemit. Be patient with me :)

Sort:  

Congratulations @ibrahim-ym! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @ibrahim-ym! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.20
TRX 0.24
JST 0.037
BTC 96071.21
ETH 3327.74
USDT 1.00
SBD 3.21