A Pocket Guide To Programming Core

The Workers

Let’s first discuss a real world example before discussing actual programming term used for The Workers.

Let’s assume you have successfully launched a product. And you are getting a lot of orders from customers in different regions. You sell that product in bulk. Let’s assume you process orders only for 100 items. Each item in an order must have label. For example PYYYYMMDD-XXX. Where ‘YYYYMMDD’ is date of shipment and ‘XXX’ is item number between 1 to 100.

Right now, your employees are manually typing or stamping each product before shipping. And you want to automate this stamping or printing process to save time and cost. What would you do. Yes, you will purchase or build a machine for this. Or you will purchase the machine but program it yourself.

How will you do that? Let’s analyze your problem first.

// Let's assume you have shipment date
Shipment Date = 02/02/2020

// We know our maximum quantity to deliver
Quantity = 100

// And every single product should have
// a label. Therefore,
Increment Counter = 1

// Start value
start = 1

// Product label format
Label Format = PYYYYMMDD-XXX

Now let’s convert it to a program and declare our variables first.

// Shipment date variable - YYYYMMDD
// YYYY -> 2020
// MM -> 02
// DD -> 02
string shipmentDate = "20200202";

// We know our maximum quantity to deliver
int quantity = 100;

// And every single product should have
// a label. Therefore,
int counter = 1;

// Start value
int start = 1;

// First item value when starting from 1
int itemNumber = 1;

// Product label format PYYYYMMDD-XXX
// Note: Most programming languages support
// string concatenation using "+" sign.
string label = "P" + shipmentDate + "-" + itemNumber;

Now as we already have our variables. Let’s assume we have printLabel function in our programming language. How will our program look like?

string shipmentDate = "20200202";

// Printing label of 1st product
int itemNumber = 001;
string labelProduct1 = "P" + shipmentDate + "-" + itemNumber;
printLabel(labelProduct1);

// Printing label of 2nd product
itemNumber = 002;
string labelProduct2 = "P" + shipmentDate + "-" + itemNumber;
printLabel(labelProduct2);

...
...
...

// Printing label of 100th product
itemNumber = 003;
string labelProduct100 = "P" + shipmentDate + "-" + itemNumber;
printLabel(labelProduct100);

Damn. This is really bad. There should be an alternate and better way to avoid this repetition. Because, that’s why we use machines and computers.

You are absolutely correct. We do have a solution for this problem in programming.

We call these Loops (or The Workers, don’t worry it’s my own definition).

So, what is a Loop? A loop is a sequence of statements or instructions that is automatically repeated until N number of times or a certain condition is reached.

I know it might be difficult to understand at this time. Let’s start building understanding by solving our problem above, that is how to avoid repetition and save both time and effort.

Let’s see what we want to achieve.

// We want to print N number of labels,
// where counter should be incremented (increased)
// by 1 for every single product or iteration.
// So our loop should look almost similar to this

// Start condition and value
start counter from 1

   // Our repeated code or statement
   call printLabel() function

// Break/Terminate/End Condition
until N which is 100

// Repetition counter
add 1 to counter after every iteration 

Yes. That’s look awesome we will be able to save at least 300-400 lines of extra and repeated code. So a loop in programming should look like this.

Start
    Check if we have met our condition
    No
        Repeat
        Increment repetition counter
    Yes
        Break or End Loop

There can be many different problems and solutions to those problems. Therefore, most of the programming languages provide different type of loops for different scenarios. Here are two traditional type of loops.

// For Loop
// for (
//        start condition,
//        end condition,
//        increment counter
//    )
// {
//    Repeated Code
// }

// While Loop
// while (
//         end condition
//       )
// {
//    Repeated Code
//    Increment counter
// }

Now let’s just convert out problem to ‘For’ loop first.

// declare shipment date variable
string shipmentDate = '20200202';

int quantity = 100;

// Our loop will start with value
// of 1. As start = 1
// It will run until start
// is less than quantity. i.e. 100
// It will add 1 to start for every
// iteration. i.e. start++
// ++ is short form of start = start + 1;
for (
        int start = 1;
        start <= quantity;
        start++
    )
{
    string label = "P" + shipmentDate 
        + "-" + start;
    printLabel(label);
}

Now let’s convert it to a While loop as well.

// declare shipment date variable
string shipmentDate = '20200202';

// While loop only need one parameter
// So we will have to declare our
// start variable outside of loop
int start = 1;

int quantity = 100;

// As we have declare start variable
// with value 1. Therefore, our
// while loop will start with value
// 1. It first check if start is
// less and equal to quantity.
// If yes or true if will print label.
// Increment start by one.
// continue printing until
// start will be equal to quantity.
// i.e. 100
while (start <= quantity)
{
    string label = "P" + shipmentDate 
        + "-" + start;
    printLabel(label);
    start++
}

I hope you have good understanding of programming loops now. If you want to learn more about loops in programming language of your choice. Please go ahead and search online or refer to a book.

Let’s move on to next chapter and learn about “The Specialists”. Please click “Next” to continue.

Image placeholder

Hi! I'm Zeeshan Elahi

I have compiled and prepared this content with love for people like me and you. And as you know it always take some time and extra cups of coffee to read, compile and prepare a course or manual. If you have like reading this content and want to say thanks, please consider supporting from more stuff like this.

2 Comments

  • flourish

    April 26, 2020 at 11:34 pm

    AԀmiring the time and effort you put into your websіte and ԁetaileԁ information you provide.
    It’s gooԁ to come across a blog every once
    in a while that isn’t tһe same outdаted rehashed informɑtion.
    Wоnderful rеad! I’ve bo᧐kmaгked your site and I’m adԀing ʏⲟᥙr RSS feeds to
    my Gooցle account.

    Reply

    • Zeeshan Elahi Author

      April 27, 2020 at 5:14 am

      Thank you. Really appreciated.

      Reply

Leave a comment

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

*

*